Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Python's subprocess.call and subprocess.run

I've been trying to understand for a while now what's the difference between subprocess.call and subprocess.run. I know the last one is new on Python 3.5 and both are based on subprocess.Popen, but I'm not able to understand the difference yet.

like image 494
Andrés Orozco Avatar asked Nov 19 '16 20:11

Andrés Orozco


People also ask

What is the difference between subprocess call and run?

I can say that you use subprocess. call() when you want the program to wait for the process to complete before moving onto the next process. In the case of subprocess. run() , the program will attempt to run all the processes at once, inevitably causing the program to crash.

What is Python subprocess call?

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.

What is subprocess run?

The Subprocess. run method takes a list of arguments. When the method is called, it executes the command and waits for the process to finish, returning a “CompletedProcess” object in the end. The “CompletedProcess” object returns stdout, stderr, original arguments used while calling the method, and a return code.

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.


1 Answers

The definition of subprocess.call() clearly mentions:

It is equivalent to: run(...).returncode (except that the input and check parameters are not supported)

As the Python 3.5's subprocess document says:

Prior to Python 3.5, these three functions (i.e. .call(), .check_call(), .check_output()) comprised the high level API to subprocess. You can now use run() in many cases, but lots of existing code calls these functions.


It is a common practice that when some functions are replaced, they are not instantly deprecated but there is a support window for them for some versions. This helps in preventing the breakage of older code when the language version is upgraded. I do not know whether .call() is going to be replaced in the future or not. But based on the document, what I know is that they are pretty much same.

like image 52
Moinuddin Quadri Avatar answered Oct 13 '22 06:10

Moinuddin Quadri