Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- pwait2.py -- multiple subprocesses without threading/multiprocessing
- '''
- import random, time
- from 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() for _ in xrange(num) ]
- # keep a 'pool' of NUM_PROCS Popen's running at one time
- NUM_PROCS = 3
- procs = spawn(NUM_PROCS)
- while True:
- print time.ctime(), 'scan'
- procs = [
- proc for proc in procs
- if proc.poll() is 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)
Advertisement
Add Comment
Please, Sign In to add comment