Python

How to use Python Subprocess with Pipes?

Python is the most common high-level language perfect for beginners. It is useful for machine learning, web development, and other functionalities. It is one of the most straightforward and accessible programming languages to help programmers write clear, precise, and simple code. It provides many built-in functions, easy-to-understand methods, and several subprocesses that allow the user to execute Linux, C, C++, or java commands directly from python. This article will discuss the use of subprocesses in python and explain how those subprocesses can be combined with pipes. So let us start.

The official python documentation provides a detailed explanation of the subprocess in python and how to use the python subprocess with pipes, and it is beneficial for understanding and development purposes. However, the complete documentation can be a bit overwhelming for beginners. Moreover, the syntax might vary from one version to another version of python, which may confuse the beginners.

Hence, we have a solution for you, and here we explain the step-by-step process of using the python subprocess with pipes. We will explain the process with the help of examples, so let us go further to see the examples but, first, let us see what a pipe is to a subprocess in python. The PIPE in python is used to send or receive data from a program that is running as a subprocess in python. To do this, we can use a combination of pipe which is basically creating the pipe, a fork is used to create the subprocess in the program, dup2 is used to force the subprocess to use the pipe as a standard input and output channel, and lastly, exec is used to execute or run the new program. Moreover, Popen and Pclose are used to open or close the program, respectively.

The benefit of using Popen and Pclose is that it suggests a simple and easy-to-use interface. However, it does not provide much flexibility because it directly uses low-level functions. Let us see the examples below.

Example 1:

The first example will explain how to encode the command that contains a pipe and a redirection. Let us see the example:

zcat f1.dat.gz f2.dat.gz | pigz > out.gz

Here is the python code that explains how to implement the Unix command with subprocess in python.

p1 = subprocess.Popen(["zcat", "f1.dat.gz", "f2.dat.gz"],

     stdout=subprocess.PIPE)

fout = open('out.gz', 'wb')

p2 = subprocess.run(['pigz'], stdin=p1.stdout, stdout=fout)

Text Description automatically generated

Note that the Popen() method is used with the first subprocess, while the run() method is used with the second call that encodes the redirections. The subsequent error will be caused if the run() method is used with the pipe itself.

Example 2:

This example will explain how a python program asks a user’s name. Then echo’s it with a greeting message and repeatedly asks for the names until the user enters the ‘exit.’ When the user enters ‘exit’ in the reply of asking the name, the if condition met with the given criteria and execution of the program stops. See the code given below.

import sys
print ("what is your name?")
for name in iter(sys.stdin.readline, ''):
    name = name[:-1]
    if name == "exit":
        break
    print ("Well, how are you {0}?".format(name))
    print ("\n What is your name?")

Text Description automatically generated

Here is the repeated output of the above code that only stops when the user enters the word ‘exit.’

Text Description automatically generated with medium confidence

Example 3:

This example is a little complicated but easy to understand and use. It will explain how to control the input and output of a program using PIPE and subprocess in python. Let us see the code first.

import subprocess
import sys
proc = subprocess.Popen(["python", "CallMyName.py "])
while proc.returncode is None:
    proc.poll()
proc = subprocess.Popen(["python", "CallMyName.py "],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.stdin.write("Alex\n")
proc.stdin.write("Jhon\n")
proc.stdin.close()
while proc.returncode is None:
    proc.poll()

print ("I am back from the child program this:\n{0}".format(proc.stdout.read()))

Text Description automatically generated

The subprocess.Popen() takes two named arguments, one is stdin and the second is stdout. Both of these arguments are optional. These arguments are used to set the PIPE, which the child process uses as its stdin and stdout. The subprocess.PIPE is passed as a constant so that either of the subprocess.Popen() or subprocess.PIPE the user specifies that they want the resultant. A child process named CallMyName.py is created in the program. Two names are passed to the CallMyName.py before sending the EOF signal to the input of the child. The mother process waits for the child process to finish before it reads the output produced by the child process. The following is the output of the above-given code.

Shape Description automatically generated with medium confidence

As you can see, it is straightforward to use PIPE with the subprocess in python. If you follow the examples given above, you can easily learn the usage of PIPE with subprocesses in python.

Conclusion:

This article is about how to use the python subprocess with PIPE. First, we briefly introduced what a PIPE is to a subprocess in python, and then we provided some examples to explain how to use the PIPE with a subprocess in python.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content

Page was generated in 0.057476997375488