Subprocess calls, are they done in parallel?

2020-05-23 09:20发布

问题:

I've been Googling for an answer to this question but nowhere seems to have one. Can anyone tell me if the subprocess module does its calls in parallel? The Python docs suggest it can be used to spawn new processes, but it doesn't mention if they are in parallel or not. If they can be done in parallel could you kindly show me an example or link me to one?

回答1:

It depends on how you use subprocess:

subprocess.call("some-program")

will block until some-program completes.

p = subprocess.Popen("some-program")

will run some-program in a separate process, in parallel with the remainder of your script.

Note that the first is simply a convenient wrapper that is equivalent to

subprocess.Popen("some-program").wait()    

output = subprocess.check_output("some-program") is basically the same as

output, stderr = subprocess.Popen("some-program").communicate()