以下代碼將引發異常並在Python 2.7和3.3中打印123。將multiprocessing.Queue放入多處理器.Queue爆炸
from multiprocessing import Queue
class Pool(object):
def __init__(self):
self.q = Queue()
p = Pool()
p.q.put(p)
print(123)
它實際上是某種形式的競爭條件的可以在這裏看到:
[email protected]:~/$ python3.3 t.py
123
Traceback (most recent call last):
File "/home/yuv/Downloads/Python-3.3.0/Lib/multiprocessing/queues.py", line 249, in _feed
[email protected]:~/$
完整的錯誤是RuntimeError: Queue objects should only be shared between processes through inheritance
和回溯完全不解釋如何/它發生在哪裏。問題的根源在於隊列中的對象無法引用隊列。我的真實用例實際上是一個工作對象和一個池對象,在這裏工作人員報告它完成了對池的Queue
的工作。所以我希望工人把自己送回工人Queue
。
我沒有使用Queue.Queue
的原因,雖然多線程對我的情況很好,但是因爲在Python 2.7中有一個錯誤讓queue.get()忽略了Ctrl-C,這只是令人討厭的。
有沒有辦法乾淨地做這個模式?
The real problem code is on codepad
我不知道怎麼有2個隊列會有所幫助。即我想排隊一個可以添加東西到隊列的對象。 (請參閱鍵盤上的代碼) – ubershmekel 2013-04-25 12:53:55