Created
April 30, 2013 01:15
-
-
Save tristanwietsma/5486029 to your computer and use it in GitHub Desktop.
Batch execution Given an list of shell commands, this script batches them out according to user-defined batch size or default CPU count.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading, os, sys | |
from multiprocessing import cpu_count | |
NUM_CPUS = cpu_count() | |
def batch_process(command_list, batch_size=NUM_CPUS): | |
iteratorlock = threading.Lock() | |
exceptions = [] | |
cmd = command_list.__iter__() | |
def runall(): | |
while True: | |
iteratorlock.acquire() | |
try: | |
try: | |
if exceptions: return | |
next_job = cmd.next() | |
finally: iteratorlock.release() | |
except StopIteration: return | |
try: os.system(next_job) | |
except Exception(next_job): | |
failure = sys.exc_info() | |
iteratorlock.acquire() | |
try: exceptions.append(failure) | |
finally: iteratorlock.release() | |
jobs = [threading.Thread(target=runall) for j in xrange(batch_size)] | |
for job in jobs: job.start() | |
for job in jobs: job.join() | |
if exceptions: | |
alfa, beta, dlta = exceptions[0] | |
raise alfa, beta, dlta |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment