Skip to content

Instantly share code, notes, and snippets.

@peiyunh
Created September 27, 2018 01:38
Show Gist options
  • Save peiyunh/d11980ac3a61d9b55cf446ad550562ee to your computer and use it in GitHub Desktop.
Save peiyunh/d11980ac3a61d9b55cf446ad550562ee to your computer and use it in GitHub Desktop.
Run commands in parallel with a limited pool
num_proc = 8
procs = []
running = []
for command in commands:
while (sum(running) >= num_proc):
for i, proc in enumerate(procs):
running[i] = (proc.poll() is None)
time.sleep(1.0)
procs.append(subprocess.Popen(command, shell=True))
running.append(True)
while (sum(running) > 0):
for i, proc in enumerate(procs):
running[i] = (proc.poll() is None)
time.sleep(1.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment