Skip to content

Instantly share code, notes, and snippets.

@clayadavis
Created November 11, 2016 17:41
Show Gist options
  • Save clayadavis/a8c07c3d9396d5ec6e5790a4fd55021c to your computer and use it in GitHub Desktop.
Save clayadavis/a8c07c3d9396d5ec6e5790a4fd55021c to your computer and use it in GitHub Desktop.
Run multiple commands in parallel
'''
Run multiple commands in parallel.
Run from CLI like
$ python purr.py commands.txt
where `commands.txt` is a text file containing one command per line.
'''
import subprocess
import sys
import tempfile
commands = open(sys.argv[1])
processes = []
stored_exception = None
try:
for cmd in commands:
cmd = cmd.strip()
if not cmd:
continue
stdout = tempfile.TemporaryFile()
stderr = tempfile.TemporaryFile()
p = subprocess.Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
processes.append( (p, stdout, stderr) )
except Exception as e:
stored_exception = e
finally:
with open('stdout.txt', 'wb') as master_stdout,\
open('stderr.txt', 'wb') as master_stderr:
for p, stdout, stderr in processes:
p.wait()
stdout.seek(0)
master_stdout.write(stdout.read())
stdout.close()
stderr.seek(0)
master_stderr.write(stderr.read())
stderr.close()
if stored_exception is not None:
raise stored_exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment