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.
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.
Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes: Queues are thread and process safe.
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 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.
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 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.
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 ...
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With