#!/usr/bin/python import subprocess import threading import time import signal spawnLock = threading.Lock() class Spawner(threading.Thread): def __init__(self, number): self.spin = True self.number = number threading.Thread.__init__(self) def run(self): child = None count = 0 prefix = str(self.number) + '-' while self.spin == True: # if access to Popen is not serialized for entire process, the empty python # process will regularly be blocked until the sleeping process exits #spawnLock.acquire() try: start = time.time() proc = (['python', '-c', 'import time;time.sleep(2)'], 1) if self.number % 2 == 1 else (['python', '-c', ''], 2) child = subprocess.Popen(proc[0]) finally: #spawnLock.release() pass child.wait() diff = time.time() - start if proc[1] == 2 and diff > 1: print 'command took too long: ' + str(diff) count = count + 1 spawned = [] subprocess._cleanup = lambda: None for i in range(3): th = Spawner(i) th.start() spawned.append(th) try: signal.pause() except: pass for th in spawned: th.spin = False th.join()