Skip to content

Instantly share code, notes, and snippets.

@sainathadapa
Created September 24, 2018 12:21
Show Gist options
  • Save sainathadapa/c1ae7fbc141f079faa4d0b9c5167ef11 to your computer and use it in GitHub Desktop.
Save sainathadapa/c1ae7fbc141f079faa4d0b9c5167ef11 to your computer and use it in GitHub Desktop.
Run bunch of commands in parallel, with an option to specify the maximum number of commands to run at a time.
import subprocess
import time
commands_to_run = ['ls -alh' for _ in range(100)]
max_procs = 16
processes = []
while (len(processes) > 0) or (len(commands_to_run) > 0):
if (len(processes) < max_procs) & (len(commands_to_run) > 0):
this_cmd = commands_to_run.pop()
print(this_cmd)
this_proc = subprocess.Popen(this_cmd, shell=True)
processes.append(this_proc)
for i in range(len(processes)):
this_proc = processes[i]
if this_proc.poll() is not None:
return_code = this_proc.poll()
print('Process exited with code: ' + str(return_code))
del processes[i]
break
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment