def testBasic(self): reap_cnt = [0] def callback(pid, status): val = reap_cnt.pop() + 1 reap_cnt.append(val) pp = PythonPrefork(on_reap_cb=callback) filename = 'test.txt' with file(filename, 'w') as fh: fh.write('0') while pp.signal_received is None: if pp.start(): continue with file(filename, 'r+') as fh: fcntl.flock(fh.fileno(), fcntl.LOCK_EX) num = int(fh.read()) + 1 fh.seek(0, os.SEEK_SET) fh.write("%d" % (num)) fcntl.flock(fh.fileno(), fcntl.LOCK_UN) if num == pp.max_workers: os.kill(pp.manager_pid, signal.SIGTERM) time.sleep(5) pp.finish() pp.wait_all_children() self.assertEqual(int(file(filename, 'r').read()), pp.max_workers) self.assertEqual(reap_cnt[0], pp.max_workers) os.remove(filename)
def main(): pp = PythonPrefork() while not pp.signal_received: if pp.start(): continue run() pp.finish() pp.wait_all_children()