concurrency - Using Python to kick off Python subprocesses without delay -


i want launch multiple instances of python script using subprocess.call, kick off script waits each complete. how prevent waiting going 1 one, without waiting previous job complete?

step = 5 n in range(5, 11, step):     subprocess.call(["python", cwd + "/" + "subprocess.py", str(n - step), str(n)]) 

that's documented behaviour of subprocess.call() can't use way. instead can use subprocess.popen().

import subprocess import os.path  processes = [] step = 5 n in range(5, 11, step):     processes.append(subprocess.popen(['python', os.path.join(cwd, 'child.py'), str(n - step), str(n)]))  p in processes:    # wait child processes terminate, avoid zombies     p.wait() 

note bad idea name file subprocess.py, if in same directory main script - import subprocess import local version, not system version. i've renamed child.py in above code.

it important parent process waits child processes. omitting can lead "zombie" processes in linux.

if using python 3 investigate use of asyncio module.


Comments