Skip to content

Instantly share code, notes, and snippets.

@thorsummoner
Created January 3, 2015 01:20
Show Gist options
  • Save thorsummoner/6cfe47a72aa3a9498bbb to your computer and use it in GitHub Desktop.
Save thorsummoner/6cfe47a72aa3a9498bbb to your computer and use it in GitHub Desktop.
import argparse
import multiprocessing
import signal
import subprocess
from pprint import pprint
globallock = multiprocessing.Lock()
class BatchSubprocess(object):
"""docstring for BatchSubprocess"""
def __init__(self, popen, items, threads=3):
super(BatchSubprocess, self).__init__()
self.popen = popen
self.items = items
self.threads = 3
try:
self.threads = int(threads)
except ValueError:
pass
results = None
pool = multiprocessing.Pool(self.threads)
procitems = list()
for item in self.items:
procitems.append(list(popen) + list(item))
try:
results = pool.map_async(_threadprocess, procitems).get(9999999)
except KeyboardInterrupt:
print('User Interupt')
pool.close()
def _threadprocess(popen):
signal.signal(signal.SIGINT, signal.SIG_IGN)
stdout = subprocess.Popen(
popen,
stdout=subprocess.PIPE
).stdout.read()
globallock.acquire()
print(stdout.decode('ascii').rstrip())
globallock.release()
return stdout
if '__main__' == __name__:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('shell', metavar='ShellCommand', type=str)
parser.add_argument('items', metavar='params_list', type=str, nargs='+')
parser.add_argument('--threads', default=3, help='Numthreads')
argp = parser.parse_args()
command = list(argp.shell.split())
c_items = list()
for item in argp.items:
c_items.append(list(item.split()))
BatchSubprocess(command, c_items, argp.threads)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment