python - wait() on a group of Popen objects -


i have number of popen objects, each representing long-running command have started. in fact, not expect these commands exit. if of them exit, want wait few seconds , restart. there good, pythonic way this?

for example:

import random subprocess import popen  procs = list() in range(10):     procs.append(popen(["/bin/sleep", str(random.randrange(5,10))])) 

a naive approach might be:

for p in procs:     p.wait()     print "a process has exited"     # restart code print "all done!" 

but not alert me first process has exited. try

for p in procs:     p.poll()     if p.returncode not none:         print "a process has exited"         procs.remove(p)         # restart code print "all done!" 

however, tight loop , consume cpu. suppose add time.sleep(1) in loop it's not busywaiting, lose precision.

i feel there should nice way wait on group of pids -- right?

  1. the "restart crashed server" task common, , shouldn't handled custom code unless there's concrete reason. see upstart , systemd , monit.

  2. the multiprocessing.pool object sounds win -- automatically starts processes, , restarts them if needed. unfortunately it's not configurable.

here's 1 solution old popen:

import random, time subprocess import popen   def work_diligently():     cmd = ["/bin/sleep", str(random.randrange(2,4))]     proc = popen(cmd)     print '\t{}\t{}'.format(proc.pid, cmd) # pylint: disable=e1101     return proc   def spawn(num):     return [ work_diligently() _ in xrange(num) ]   num_procs = 3 procs = spawn(num_procs) while true:     print time.ctime(), 'scan'     procs = [          proc proc in procs         if proc.poll() none     ]     num_exited = num_procs - len(procs)     if num_exited:         print 'uhoh! restarting {} procs'.format(num_exited)         procs.extend( spawn(num_exited) )     time.sleep(1) 

output:

    2340    ['/bin/sleep', '2']     2341    ['/bin/sleep', '2']     2342    ['/bin/sleep', '3'] mon jun  2 18:01:42 2014 scan mon jun  2 18:01:43 2014 scan mon jun  2 18:01:44 2014 scan uhoh! restarting 2 procs     2343    ['/bin/sleep', '3']     2344    ['/bin/sleep', '2'] mon jun  2 18:01:45 2014 scan uhoh! restarting 1 procs     2345    ['/bin/sleep', '2'] mon jun  2 18:01:46 2014 scan uhoh! restarting 1 procs     2346    ['/bin/sleep', '2'] mon jun  2 18:01:47 2014 scan uhoh! restarting 2 procs     2347    ['/bin/sleep', '3']     2349    ['/bin/sleep', '2'] 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -