Python Subprocess Module | Subprocess vs Multiprocessing

Rinu Gour
4 min readOct 12, 2018

--

Python Subprocess Module | Subprocess vs Multiprocessing

Python Subprocess vs 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 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:

Have a look at Python Modules

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

a. Syntax

It has the following syntax-

  1. subprocess.call(args, *, 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

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

1

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

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.

Have a look at Python Decision Making Statements

a. Syntax

It has the following syntax-

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

b. Examples

Time for some examples of Python Subprocess run().

  1. >>> subprocess.run([‘ls’,’-l’],shell=True)

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

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-

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

  1. >>> subprocess.check_call(‘true’,shell=True)

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.

Do you know about Python Syntax and Semantics

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-

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

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

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-

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

b. Examples

Below is the example of Python Subprocess Communicate

  1. >>> p=subprocess.Popen([“echo”,”hello world”],stdout=subprocess.PIPE,shell=True)
  2. >>> 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

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-

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

  1. >>> proc=subprocess.Popen([‘echo’,’”to stdout”’],stdout=subprocess.PIPE,shell=True)
  2. >>> stdout_value=proc.communicate()[0]
  3. >>> 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.

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?

--

--

Rinu Gour

Data Science Enthusiast | Research writer | Blogger | Entrepreneur