Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Broken Pipe Error in Python

  • Last Updated : 23 Sep, 2021

In this article, we will discuss Pipe Error in python starting from how an error is occurred in python along with the type of solution needed to be followed to rectify the error in python. So, let’s go into this article to understand the concept well. 

With the advancement of emerging technologies in the IT sector, the use of programming language is playing a vital role. Thus the proper language is considered for the fast executions of the functions. In such a case, Python emerges as the most important language to satisfy the needs of the current problem execution because of its simplicity and availability of various libraries. But along with the execution, the errors during the execution also comes into existence and it becomes difficult for the programmers to rectify the errors for the processing of the problem.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

The Emergence of Broken Pipe Error

A broken Pipe Error is generally an Input/Output Error, which is occurred at the Linux System level. The error has occurred during the reading and writing of the files and it mainly occurs during the operations of the files. The same error that occurred in the Linux system is EPIPE, but every library function which returns its error code also generates a signal called SIGPIPE, this signal is used to terminate the program if it is not handled or blocked. Thus a program will never be able to see the EPIPE error unless it has handled or blocked SIGPIPE.



Python interpreter is not capable enough to ignore SIGPIPE by default, instead, it converts this signal into an exception and raises an error which is known as IOError(INPUT/OUTPUT error) also know as ‘Error 32’ or Broken Pipe Error.

Broken Pipe Error in Python terminal

python <filename>.py | head

This pipeline code written above will create a process that will send the data upstream and a process that reads the data downstream. But when the downstream process will not be able to read the data upstream, it will raise an exception by sending SIGPIPE signal to the upstream process. Thus upstream process in a python problem will raise an error such as IOError: Broken pipe error will occur.

Example:

Python3




for i in range(4000):
    print(i)

When we run this file from unix commands:

python3 main.py | head -n3000

Procedure to avoid Broken Pipe Error

Approach 1: To avoid the error we need to make the terminal run the code efficiently without catching the SIGPIPE signal, so for these, we can add the below code at the top of the python program.



from signal import signal, SIGPIPE, SIG_DFL  
signal(SIGPIPE,SIG_DFL) 

Python3




from signal import signal, SIGPIPE, SIG_DFL  
signal(SIGPIPE,SIG_DFL)
  
for i in range(4000):
    print(i)

Output:

0
1
20
1
2
3
4
5
6
7
8
9
3
4
5
6
7
8
9

Explanation:

The above code which is placed on the top of the python code is used to redirect the SIGPIPE signals to the default SIG_DFL signal, which the system generally ignores so that the rest part of the code can be executed seamlessly. But Approach 11 is not effective because in the Python manual on the signal library, which is mentioned that this type of signal handling should be avoided and should not be practiced in any part of the code. So for this reason we will go for the second approach.

Approach 2: We can handle this type of error by using the functionality of try/catch block which is already approved by the python manual and is advised to follow such procedure to handle the errors.

import sys, errno  
try:  
   # INPUT/OUTPUT operation #
except IOError as e:  
   if e.errno == errno.EPIPE:  
       # Handling of the error  

Example:

Python3




import sys
import errno
  
try:
    for i in range(4000):
        print(i)
except IOError as e:
    if e.errno == errno.EPIPE:
      pass
       # Handling of the error

Output:

0
1
2
3
4
5
6
7
8
9

Explanation:

In the above piece of code, we have used the built-in library of python which is the Sys and Errno module, and use the try/catch block in order to catch the raised SIGPIPE exception and handle it before it stops the program execution.




My Personal Notes arrow_drop_up
Recommended Articles
Page :