#!/usr/bin/python try: from argparse import ArgumentParser, RawTextHelpFormatter except: sys.exit('argparse module is required (available in Python 2.7.2+ or Python 3.2.1+)') import sys, os, re from multiprocessing import Process, Queue class Worker(Process): def __init__(self, queue): Process.__init__(self) self.queue = queue def run(self): while True: try: cmd = self.queue.get() if cmd is None: break else: os.system(cmd) except KeyboardInterrupt: break def run(cmds, ncpu): try: jobs = [] queue = Queue() for i in cmds: queue.put(i) for i in range(ncpu): p = Worker(queue) p.start() jobs.append(p) queue.put(None) for j in jobs: j.join() except KeyboardInterrupt: sys.exit(1) if __name__ == '__main__': parser = ArgumentParser(description='''Run input commands in parallel. Example: "cat jobs.sh | {} --jobs 8"'''.format(sys.argv[0]), formatter_class=RawTextHelpFormatter) parser.add_argument('-j','--jobs', metavar = 'N', type = int, default = 1, help = 'number of jobs') args = parser.parse_args() try: cmds = [re.sub('\r\n|(\s*);(\s*)','\n', x) for x in sys.stdin.readlines() \ if not x.startswith('#') and len(x.strip()) > 0] except KeyboardInterrupt: sys.exit("Standard input stream expected") run(cmds, args.jobs if args.jobs > 1 else 1)