Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess calls, are they done in parallel?

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?

like image 368
iNoob Avatar asked Mar 27 '14 20:03

iNoob


People also ask

Does subprocess run in parallel?

We can use subprocess module to create multiple child processes and they are run in parallel. First, we search the current directory and obtain a list of all the compressed files. Next, we create a list of the sequence of program arguments, each list element corresponding to each file.

What is a subprocess call?

Subprocess call(): Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is: subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) # Run the command described by args.

What is the difference between subprocess call and run?

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. subprocess. call() is part of the Older high-level API (Prior to Python 3.5).

What does subprocess call () python do?

The Python subprocess call() function returns the executed code of the program. If there is no program output, the function will return the code that it executed successfully. It may also raise a CalledProcessError exception.


1 Answers

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()
like image 132
chepner Avatar answered Sep 21 '22 15:09

chepner