多进程中错误 EOFError: EOF when reading a line
[ python multiprocessing.Pipe 多进程管道的使用 ] 三种常见报错
from multiprocessing import Pipe
sed, rec = Pipe()
# 依次发送两条数据
sed.send([1111])
sed.send([1112])
1,如果管道-发送端 关闭了,接收端还去接收的话,会在接收完管道中残存的数据之后,报EOF的错误:raise EOFError
sed.close() # 管道-发送端关闭
while True:
c = rec.recv() # 管道-接收端接收
print(c)
# 输出
[1111]
[1112]
Traceback (most recent call last):
File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py", line 301, in _recv_bytes
ov, err = _winapi.ReadFile(self._handle, bsize,
BrokenPipeError: [WinError 109] 管道已结束。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/user1/pipe_test.py", line 12, in <module>
c = rec.recv()
File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py", line 250, in recv
buf = self._recv_bytes()
File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py", line 321, in _recv_bytes
raise EOFError
EOFError
2,如果管道中没有数据了,接收端还去接收的话,会卡死。没有任何反应和提示,就卡住
while True:
c = rec.recv()
print(c)
# 输出
[1111]
[1112] # 至此管道中的数据已经接收完,没有数据了
(...卡住,没有报错没有输出,很恐怖!!!!)
3,如果管道-发送端 关闭了,发送端还去发送的话,会报OS错误:OSError: handle is closed
sed.close()
while True:
sed.send([3333])
# 输出
Traceback (most recent call last):
File "C:/user1/pipe_test.py", line 15, in <module>
sed.send([3333])
File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py", line 204, in send
self._check_closed()
File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py", line 136, in _check_closed
raise OSError("handle is closed")
OSError: handle is closed
4. 接收端关闭了,发送端还在发送
会报错:BrokenPipeError: [WinError 232] 管道正在被关闭。 / BrokenPipeError: [Errno 32] Broken pipe
Windows :
Traceback (most recent call last):
File "C:/user1/pipe_test.py", line 20, in <module>
sed.send([3333])
File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "C:\Users\user1u\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py", line 280, in _send_bytes
ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True)
BrokenPipeError: [WinError 232] 管道正在被关闭。
Linux Ubuntu:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "/usr/lib/python3.6/multiprocessing/connection.py", line 205, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/usr/lib/python3.6/multiprocessing/connection.py", line 410, in _send_bytes
self._send(header + buf)
File "/usr/lib/python3.6/multiprocessing/connection.py", line 367, in _send
n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
5. 只发送,不接收
发送一定量的数据之后会卡住,没有任何报错和提示,卡住不动!!
在使用python多进程Multiprocessing进行多进程间的交换数据,常见的有两种:Queue和Pipe。和FIFO的队列一样,管道也是先进先出的。
处理方式:
1,如果在进程处理时不应该关闭相应的管道,那就检查代码把关闭管道的如xx.close()的代码去掉试试
2,如果是需要解决的异常:多进程处理的时候没有处理好异常情况,可以使用try。。。except OSError或try。。。except EOFError来接收异常并进行下一步处理
3,多个进程同时对管道进行访问可能会导致竞争,可以考虑改用(管道+锁)的队列处理方式。参考:https://blog.csdn.net/qxqxqzzz/article/details/104280712
————————————————
版权声明:本文为CSDN博主「城俊BLOG」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qxqxqzzz/article/details/104384920