問題描述
在 python2.7 中,multiprocessing.Queue 在從函數(shù)內(nèi)部初始化時會引發(fā)錯誤.我提供了一個重現(xiàn)問題的最小示例.
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()
拋出下面的斷管錯誤
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
我無法解釋原因.我們不能從函數(shù)內(nèi)部填充 Queue 對象肯定會很奇怪.
I am unable to decipher why. It would certainly be strange that we cannot populate Queue objects from inside a function.
推薦答案
這里發(fā)生的是,當你調(diào)用 main()
時,它會創(chuàng)建 Queue
,放入 10對象并結(jié)束函數(shù),垃圾收集其內(nèi)部的所有變量和對象,包括 Queue
.但是您收到此錯誤是因為您仍在嘗試發(fā)送 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
.
來自文檔文檔:
"當一個進程第一次將一個項目放入隊列時,一個 feeder 線程是開始將對象從緩沖區(qū)傳輸?shù)焦艿乐?"
"When a process first puts an item on the queue a feeder thread is started which transfers objects from a buffer into the pipe."
由于 put()
是在另一個 Thread 中進行的,它不會阻塞腳本的執(zhí)行,并允許在完成之前結(jié)束 main()
函數(shù)隊列操作.
As the put()
is made in another Thread, it is not blocking the execution of the script, and allows to ends the main()
function before completing the Queue operations.
試試這個:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import multiprocessing
import time
def main():
q = multiprocessing.Queue()
for i in range(10):
print i
q.put(i)
time.sleep(0.1) # Just enough to let the Queue finish
if __name__ == "__main__":
main()
應(yīng)該有一種方法可以加入
隊列或阻止執(zhí)行,直到將對象放入Queue
,您應(yīng)該查看文檔.
There should be a way to join
the Queue or block execution until the object is put in the Queue
, you should take a look in the documentation.
這篇關(guān)于multiprocessing.Queue 的管道損壞錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!