Created
March 1, 2012 10:08
-
-
Save sveneisenschmidt/1948691 to your computer and use it in GitHub Desktop.
Parallel execution of scripts with Python
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 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