Skip to content

Instantly share code, notes, and snippets.

@sveneisenschmidt
Created March 1, 2012 10:08
Show Gist options
  • Save sveneisenschmidt/1948691 to your computer and use it in GitHub Desktop.
Save sveneisenschmidt/1948691 to your computer and use it in GitHub Desktop.
Parallel execution of scripts with Python
import subprocess
import os
import time
import pprint
import sys
steps = [
[
['sh', './thread-sleep-5.sh'],
['sh', './thread-sleep-3.sh'],
['sh', './thread-sleep-10.sh'],
],
[
],
[
['sh', './thread-sleep-10.sh'],
['sh', './thread-sleep-3.sh'],
]
]
def main(steps):
for i, step in enumerate(steps):
msg = "Step %s | Concurrent threads %s" % (i+1, len(step))
if len(step) > 0:
pprint.pprint(msg)
run(step)
else:
pprint.pprint(msg + " | Empty, Skipped")
def run(step):
processes = set()
for call in step:
processes.add(subprocess.Popen(call))
wait(processes)
def wait(procs):
while len(procs) > 0:
procs = [p for p in procs if p.poll() == None]
time.sleep(0.5)
main(steps)
sys.exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment