Python

Python Pipe Example

Python is a feature-rich language that supports all the latest as well as traditional programming concepts. The usage of pipes comes under the concept of inter-process communication, in which two or more processes exchange data or information with each other. This happens only when two or more processes are running simultaneously hence depicting the functionality of parallel processing. To learn why you should use Python pipes and understand how they are implemented in this programming language, you will have to read this article thoroughly.

What is a Pipe in Python?

The process that passes on this information writes to the pipe, whereas the process that receives this information reads from the pipe. Just like other programming languages, Python also supports the implementation of pipes.

Why should I use a Pipe in Python?

We all know that inter-process communication is a very important programming concept. Since the concept of parallel processing was introduced, the functionality and power of computers have expanded to a very large extent. This concept allowed us to run multiple processes at a time that can work independently. However, we sometimes want one process to convey some information to another that needs further processing. This is where the pipes in Python come in handy.

A pipe in Python can easily be used to transmit information from one end to another. This allows two processes to exchange data with each other very conveniently. The only thing that is needed to be taken care of while implementing pipes in Python or, for that matter, in any other programming language is that once a process is writing to the pipe, the read file descriptor must be blocked. Similarly, when a process is reading from the pipe, the write file descriptor should be blocked. This is done to ensure data integrity and that the data is synchronized between the reading and writing processes.

To throw further light on the implementation of pipes in Python, you should go through the following example that we have formulated for you to learn the usage of pipes in Python effectively.

Note: You can use the Spyder IDE to implement a program in Python by using either Windows 10 or any distribution of Linux; however, the “os.fork()” function that is used in the following example is only supported by Linux. Therefore, you will not be able to implement the following code in Windows 10.

Example of Using a Pipe in Python:

For depicting the usage of a pipe in Python, we wanted to implement a code that is capable of enabling communication between a parent process and a child process by making use of a pipe. To facilitate such communication, you will have to take a look at the following code:

In this Python code for pipe implementation, we have first imported Python’s “os” module, which will facilitate this implementation. Then, we have created a pipe with the “os.pipe()” function and have assigned it to the two file descriptors “r” and “w” for reading and writing data, respectively. After that, we wanted to spawn a child process with the process ID, i.e., pid that can be done with the help of the “os.fork()” function. Then, we have an “if” statement that operates on the PID greater than “0” i.e., if the PID is that of a parent process, only then this “if” block will be executed. Within this “if” block, the parent process first blocks the “read” file descriptor, i.e., r, so that the parent process can easily write to the pipe without any interruptions. This is done by making use of the “os.close(r)” function.

After that, we wanted to notify the user through the console that the parent process is now writing to the pipe for which we have displayed a message using the “print” command. Then, we have a “text” variable to which we have assigned the text that the parent process wants to write to the pipe.

After that, we have called the “os.write()” function. This function accepts two arguments, i.e., the write file descriptor and the text to be written to the pipe. When this function is called, the text will automatically be written to the pipe. After that, we wanted to display the text written onto the pipe on our console, which we will do with the help of the “print” function by decoding the text variable.

Then, there is an “else” statement that will work if the PID is not greater than “0” i.e., the PID is that of a child process; only then this “else” block will be executed. Within this “else” block, the child process first blocks the “write” file descriptor, i.e., w, to easily read the text written onto the pipe without any modifications. This is done by making use of the “os.close(w)” function.

After that, we wanted to notify the user through the console that the child process is now reading from the pipe for which we have displayed a message using the “print” command. Then, we have assigned the value of the opened read file descriptor “r” to our initially declared read file descriptor. Finally, we wanted to display the text read from the pipe by our child process on our console by using the “print” function. After writing this Python code in the Spyder IDE, we have simply saved it and then executed it.

Once this code was executed, its output turned out to be somewhat shown in the image below:

In this output, you can see that the first message that appeared on the console was “Parent process writes:” which implies that currently, the read file descriptor is blocked as the parent process is writing to the pipe. The second message that appeared on the console was “Written Text: Welcome my child!” which represents the text written to the pipe by the parent process. Then, the third message that was displayed in the output was “Child process reads:” which implies that the write file descriptor is blocked currently as the child process is reading from the pipe. Finally, the fourth message that was displayed on the console was “Read Text: Welcome my child!” which simply represents the text that was read from the pipe by the child process.

In this way, you can work with pipes in Python and create even more complex examples in which you can depict a full-fledged communication between the parent and child processes.

Conclusion:

We talked briefly about the pipes in Python at the beginning of this article. Then we went a little further by explaining how they work and why we should be using them in the first place. After that, we explained how the pipes could be implemented in Python with the help of a useful example in which one process conveys some information to another. Once you go through this example, you will quickly grasp how pipes are implemented in Python, and then you will be able to implement more complex programs in which two processes communicate with each other.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.