Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broken pipe error with multiprocessing.Queue

In python2.7, multiprocessing.Queue throws a broken error when initialized from inside a function. I am providing a minimal example that reproduces the problem.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import multiprocessing

def main():
    q = multiprocessing.Queue()
    for i in range(10):
        q.put(i)

if __name__ == "__main__":
    main()

throws the below broken pipe error

Traceback (most recent call last):
File "/usr/lib64/python2.7/multiprocessing/queues.py", line 268, in _feed
send(obj)
IOError: [Errno 32] Broken pipe

Process finished with exit code 0

I am unable to decipher why. It would certainly be strange that we cannot populate Queue objects from inside a function.

like image 444
hAcKnRoCk Avatar asked Apr 01 '16 14:04

hAcKnRoCk


People also ask

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

Is multiprocessing queue process safe?

Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes: Queues are thread and process safe.

How do I clear a multiprocessing queue?

There is no direct way of clearing a multiprocessing. Queue . I believe the closest you have is close() , but that simply states that no more data will be pushed to that queue, and will close it when all data has been flushed to the pipe.

What is pipe in multiprocessing?

What is a Pipe. In multiprocessing, a pipe is a connection between two processes in Python. It is used to send data from one process which is received by another process. Under the covers, a pipe is implemented using a pair of connection objects, provided by the multiprocessing.

How to avoid broken pipe error in Python?

Thus upstream process in a python problem will raise an error such as IOError: Broken pipe error will occur. 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.

What happens when you call main () on a queue?

What happens here is that when you call main (), it creates the Queue, put 10 objects in it and ends the function, garbage collecting all of its inside variables and objects, including the Queue. BUT you get this error because you are still trying to send the last number in the Queue.

How to fix broken pipe error with SSH?

Fixing broken pipe error with SSH 1 If you connect to multiple servers via SSH, set it on your machine. 2 If you are a sysadmin and several of users complain about frequent SSH connection disconnect, you may set it on the... More ...

What does'write failed broken pipe'or'connection closed by remote host'mean?

On some systems, it will display 'Write failed: Broken pipe' or 'Connection closed by remote host'. Let's see what causes this error and how to go about keeping your SSH connection alive. As you may have guessed, the SSH connection is closed because of inactivity.


1 Answers

When You fire up Queue.put(), implicit thread is started to deliver data to a queue. Meanwhile, main application is finished and there is no ending station for the data (queue object is garbage-collected).

I would try this:

from multiprocessing import Queue

def main():
    q = Queue()
    for i in range(10):
        print i
        q.put(i)
    q.close()
    q.join_thread()

if __name__ == "__main__":
    main()

join_thread() ensures, all data in the buffer has been flushed. close() must be called before join_thread()

like image 166
Peter Svac Avatar answered Oct 16 '22 23:10

Peter Svac