Python Subprocess Module | Subprocess vs Multiprocessing

Python course with 57 real-time projects - Learn Python

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

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

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:

  • 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
It has the following syntax-

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

2. Examples
Let’s take a few simple examples of Subprocess Call in Python.

>>> subprocess.call('exit 1',shell=True)

Output

1
>>> subprocess.call('ls -l',shell=True)

Output

1

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. 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. Examples
Time for some examples of Python Subprocess run().

>>> subprocess.run(['ls','-l'],shell=True)

Output

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.

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

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

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

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

Output

Traceback (most recent call last):
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. 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. Examples
Now, the example of Python Subprocess check_output

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

Output

b'”Hello World!”\r\n’

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

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

2. Examples
Below is the example of Python Subprocess Communicate

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

Output

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

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

‘b\'”\\\\”to stdout\\\\””\\r\\n\”

So, this was all in Python Subprocess Module. Hope you like our explanation.

Python Interview Questions on Subprocess Module

  1. What is subprocess module in Python?
  2. How does Python subprocess work?
  3. How do you start and close a subprocess in Python?
  4. How do you run a command in subprocess in Python?
  5. 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.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

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