python - asynchronous subprocess with timeout -


i have problem spawning asynchronous subprocesses timeout in python 3.

what want achieve: want spawn multiple processes asynchronously without waiting results want assured every spawned process end within given timeout.

i have found similar problems here: using module 'subprocess' timeout , asynchronous background processes in python? not solve issue.

my code looks this. have command class suggested in using module 'subprocess' timeout :

class command(object):   def __init__(self, cmd):     self.cmd = cmd     self.process = none    def run(self, timeout):     def target():       print('thread started')       args = shlex.split(self.cmd)       self.process = subprocess.popen(args, shell=true)       self.process.communicate()       print('thread finished')      thread = threading.thread(target=target)     thread.start()      thread.join(timeout)     if thread.is_alive():       print('terminating process')       self.process.terminate()       thread.join() 

and when want spawn subprocesses:

for system in systems:   service in to_spawn_system_info:     command_str = "cd {0} && python proc_ip.py {1} {2} 0 2>>{3}".format(home_dir,         service, system, service_log_dir)     command = command(command_str)     command.run(timeout=60) 

when run output seems wait every command spawn , end. get

thread started thread finished thread started thread finished thread started thread finished thread started thread finished 

so question doing wrong? starting wonder if possible spawn process , limit execution timeout.

why need this? spawner script run in cron. executed every 10 minutes , has spawn 20 subprocesses. want guarantee every subprocess end before script run again cron.

as mentioned previously, call process.communicate() making code wait completion of subprocess. however, if remove communicate() call, thread exit after spawning process, causing thread.join() call exit soon, , you'll kill off subprocess prematurely. want without polling or busy waiting, can set timer kill process (and runner thread) after timeout if process has not yet finished:

class command(object):   def __init__(self, cmd):     self.cmd = cmd     self.process = none    def run(self, timeout):     def target():       print('thread started')       # may want/need skip shlex.split() when using shell=true       # see popen() constructor docs on 'shell' argument more detail.       args = shlex.split(self.cmd)       self.process = subprocess.popen(args, shell=true)       self.timer.start()       self.process.wait()       self.timer.cancel()      def timer_callback():         print('terminating process (timed out)')         self.process.terminate()      thread = threading.thread(target=target)     self.timer = threading.timer(timeout, timer_callback)     thread.start() 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -