Skip to content

Instantly share code, notes, and snippets.

@baverman baverman/throng
Created Jul 22, 2016

Embed
What would you like to do?
#!/usr/bin/env python
import os
import time
import argparse
import signal
from subprocess import Popen
class Pool(object):
def __init__(self, process_count, args):
self.process_count = process_count
self.args = args
self.pool = {}
self.alive = True
def handler(self, signal, frame):
self.alive = False
def fill(self):
for _ in range(self.process_count - len(self.pool)):
p = Popen(self.args)
self.pool[p.pid] = p
def wait(self):
while self.alive:
result = os.waitpid(-1, os.WNOHANG)
if result == (0, 0):
time.sleep(1)
else:
self.pool.pop(result[0])
self.fill()
def shutdown(self):
for p in self.pool.values():
try:
p.terminate()
except OSError:
pass
for p in self.pool.values():
p.wait()
def run(self):
signal.signal(signal.SIGINT, self.handler)
signal.signal(signal.SIGTERM, self.handler)
self.fill()
self.wait()
self.shutdown()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-j', type=int, default=1, metavar='process_count')
args, rest = parser.parse_known_args()
pool = Pool(args.j, rest)
pool.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.