Python Subprocess Module | Subprocess vs Multiprocessing

Don't become Obsolete & get a Pink Slip
Follow DataFlair on Google News & Stay ahead of the game

1. Objective – Python Subprocess Module

Last, we talked about Multiprocessing in Python. Today, we will see 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 Subprocess Module in Python. At last, we are going to understand all with the help of syntax and example.
So, let’s start the Python Subprocess Module tutorial.

Python Subprocess Module | Subprocess vs Multiprocessing

Python Subprocess Module | Subprocess vs Multiprocessing

2. Python Subprocess vs Multiprocessing

Seems like both help us facilitate concurrency or parallel programming. So what sets them apart?
You must check Python Generator vs Iterator

  • 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.
Python Subprocess Module | Subprocess vs Multiprocessing

Subprocess vs Multiprocessing

  • 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 of the likes of the threading module.

3. What is 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:
Have a look at Python Modules

  • os.system
  • os.spawn and related functions
  • os.popen and related functions
  • popen2*
  • commands*

4. 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.

a. Syntax
It has the following syntax-, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

b. Examples
Let’s take a few simple examples of Subprocess Call in Python.
Let’s revise Python Operators

>>>'exit 1',shell=True)


>>>'ls -l',shell=True)

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.

5. Python Subprocess run()

Like call(), this function runs a command and returns a CompletedProcess instance.
Have a look at Python Decision Making Statements
a. Syntax
It has the following syntax-, *, 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)

b. Examples
Time for some examples of Python Subprocess run().


CompletedProcess(args=[‘ls’, ‘-l’], returncode=1)
This is the same command we saw in call(). Note how we mention shell=True; also note this returns a CompletedProcess instance.
You must read about Python Variables

6. 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.
a. Syntax
We have the following syntax-

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)

b. Examples 
Let’s take a look at Python Subprocess check_call example

>>> subprocess.check_call('true',shell=True)

Traceback (most recent call last):
 File “<pyshell#3>”, line 1, in <module>
 File “C:\Users\Ayushi\AppData\Local\Programs\Python\Python37-32\lib\”, 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.
Do you know about Python Syntax and Semantics

7. 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.
a. 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)

b. Examples
Now, the example of Python Subprocess check_output

>>> subprocess.check_output(["echo","Hello World!"],shell=True)

b'”Hello World!”\r\n’
In this example, we print the string Hello World! to the console.
Have a look at Python Exceptional Handling

8. 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).
a. Syntax
Take a look at the syntax-

Popen.communicate(input=None, timeout=None)

b. Examples
Below is the example of Python Subprocess Communicate

>>> p=subprocess.Popen(["echo","hello world"],stdout=subprocess.PIPE,shell=True)
>>> p.communicate()

(b'”hello world”\r\n’, None)
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.
Let’s revise Python Multithreading

9. 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.
a. 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)

b. 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)

‘b\'”\\\\”to stdout\\\\””\\r\\n\”
Let’s take a tour to Python Network Programming
So, this was all in Python Subprocess Module. Hope you like our explanation.

10. Conclusion – Module in Python Subprocess

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. Is this explanation helps you, give us your feedback in the comments?
See also –
Generate Python Random Numbers
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.