Python Subprocess Module | Subprocess vs Multiprocessing
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Imagine the Subprocess as a remote control for your computer. While Multiprocessing helps Python do more work, Subprocess lets Python command other programs to do the work for it.
Last, we talked about Multiprocessing in Python. Today, we will see the Python Subprocess Module.
Moreover, we will discuss Subprocess vs Multiprocessing in Python. Also, we will learn call, run, check call, check output, communicate, and popen in the Subprocess Module in Python.
So, let’s start the Python Subprocess Module tutorial.
What is the difference between Python Subprocess & Multiprocessing?
Seems like both help us facilitate concurrency or parallel programming. So what sets them apart?
- Subprocess- The subprocess module comes in handy when we want to run and control other programs that we can run with the command line, too. It lets us integrate external programs into Python code.
- Multiprocessing- The multiprocessing module is something we’d use to divide tasks we write in Python over multiple processes. This lets us make better use of all available processors and improves performance. This module has an API similar to the threading module.
What is the Python Subprocess Module?
Are you through telling between the two? Okay. Time to tell you about subprocess.
This module lets you spawn new processes, connect to their input/error/output pipes, and acquire their return codes.
It finds its proposal in PEP 324 for version 2.4 and replaces the following modules/ functions in Python:
- os.system
- os.spawn and related functions
- os.popen and related functions
- popen2*
- commands*
Python Subprocess Call()
The call() function from the subprocess module lets us run a command, wait for it to complete, and get its return code.
1. Syntax of Python Subprocess Call()
It has the following syntax-
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
2. Examples of Python Subprocess Call()
Let’s take a few simple examples of Subprocess Call in Python.
>>> subprocess.call('exit 1',shell=True)Output
>>> subprocess.call('ls -l',shell=True)Output
Since we set the shell to True, this function treats this as a complete command and runs it.
This is a command that lists out all files and folders in the current directory.
Note that 1 is the return code, not the output of the command’s execution. Here, it marks success.
Python Subprocess run()
Like call(), this function runs a command and returns a CompletedProcess instance.
1. Python Subprocess run() Syntax
It has the following syntax-
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None)
2. Python Subprocess run() Examples
Time for some examples of Python Subprocess run().
>>> subprocess.run(['ls','-l'],shell=True)
Output
This is the same command we saw in call(). Note how we mention shell=True; also note this returns a CompletedProcess instance.
Python Subprocess check_call()
A call to this function runs the command with the arguments, waits for it to complete, then gets the return code.
If zero, it returns; else it raises CalledProcessError. Such an object holds the return code in the returncode attribute.
1. Python Subprocess check_call() Syntax
We have the following syntax-
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)
2. Python Subprocess check_call() Examples
Let’s take a look at the Python Subprocess check_call example
>>> subprocess.check_call('true',shell=True)Output
File “<pyshell#3>”, line 1, in <module>
subprocess.check_call(‘true’,shell=True)
File “C:\Users\Ayushi\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py”, line 328, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command ‘true’ returned non-zero exit status 1. For the false command, it always returns an error.
Python Subprocess check_output()
This function runs the command with the arguments and returns the output.
So far, the output was bound to the parent process, and we couldn’t retrieve it.
For a non-zero return code, it raises a CalledProcessError, which has the return code in the returncode attribute.
1. Python Subprocess check_output() Syntax
It has the following syntax-
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=False, timeout=None)
2. Python Subprocess check_output() Examples
Now, the example of Python Subprocess check_output
>>> subprocess.check_output(["echo","Hello World!"],shell=True)
Output
In this example, we print the string Hello World! to the console.
Python Subprocess Communicate()
This interacts with the process and sends data to stdin.
It reads data from stdout and stderr until it reaches the end-of-file and waits for the process to terminate.
What it returns is a tuple (stdout_data, stderr_data).
1. Python Subprocess Communicate() Syntax
Take a look at the syntax-
Popen.communicate(input=None, timeout=None)
2. Python Subprocess Communicate() Examples
Below is an example of Python Subprocess Communication
>>> p=subprocess.Popen(["echo","hello world"],stdout=subprocess.PIPE,shell=True) >>> p.communicate()
Output
Here, we use Popen to execute a child program in a new process. We will see this next.
Meanwhile, we read the input and output from the process using communicate().
Here, stdout is the process output. In case there’s an error, we populate stderr.
Python Subprocess Popen()
Popen is a constructor from the subprocess class that executes a child program in a new process.
This class uses the Windows CreateProcess() function, and Popen() lets us start a process.
1. Python Subprocess Popen() Syntax
We have the following syntax-
class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None, text=None)
2. Python Subprocess Popen() Examples
Let’s try the echo command with this Python Subprocess Popen example.
>>> proc=subprocess.Popen(['echo','"to stdout"'],stdout=subprocess.PIPE,shell=True) >>> stdout_value=proc.communicate()[0] >>> repr(stdout_value)
Output
So, this was all in the Python Subprocess Module. Hope you like our explanation.
Python Interview Questions on Subprocess Module
- What is the subprocess module in Python?
- How does Python subprocess work?
- How do you start and close a subprocess in Python?
- How do you run a command in a subprocess in Python?
- How do you run a Python subprocess in Python?
Conclusion
Hence, in this Python Subprocess Module, we saw the difference between subprocess and multiprocessing.
You are also no longer uninitiated to conventional functions from subprocess, the likes of call(), run(), check_output(), and Popen().
Also, we understood the complete concept with the help of syntax and examples.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google



