問題描述
我正在嘗試使用多處理編寫 Python 2.6 (OSX) 程序,并且我想用超過默認的 32767 個項目來填充隊列.
I'm trying to write a Python 2.6 (OSX) program using multiprocessing, and I want to populate a Queue with more than the default of 32767 items.
from multiprocessing import Queue
Queue(2**15) # raises OSError
Queue(32767)
工作正常,但任何更大的數(shù)字(例如 Queue(32768)
)都會失敗,并出現(xiàn) OSError: [Errno 22] Invalid argument代碼>
Queue(32767)
works fine, but any higher number (e.g. Queue(32768)
) fails with OSError: [Errno 22] Invalid argument
這個問題有解決辦法嗎?
Is there a workaround for this issue?
推薦答案
一種方法是使用自定義類包裝您的 multiprocessing.Queue
(僅在生產(chǎn)者端,或從消費者透明看法).使用它,您可以將要分派到您正在包裝的 Queue
對象的項目排隊,并且僅將本地隊列(Python list()
對象)中的內(nèi)容提供給multiprocess.Queue
隨著空間變得可用,當 Queue
已滿時進行異常處理以節(jié)流.
One approach would be to wrap your multiprocessing.Queue
with a custom class (just on the producer side, or transparently from the consumer perspective). Using that you would queue up items to be dispatched to the Queue
object that you're wrapping, and only feed things from the local queue (Python list()
object) into the multiprocess.Queue
as space becomes available, with exception handling to throttle when the Queue
is full.
這可能是最簡單的方法,因為它對您的其余代碼的影響應(yīng)該最小.自定義類的行為應(yīng)該像隊列一樣,同時將底層的 multiprocessing.Queue
隱藏在您的抽象后面.
That's probably the easiest approach since it should have the minimum impact on the rest of your code. The custom class should behave just like a Queue while hiding the underlying multiprocessing.Queue
behind your abstraction.
(一種方法可能是讓您的生產(chǎn)者使用線程,一個線程來管理從線程 Queue
到您的 multiprocessing.Queue
和任何其他線程實際上只是喂入線程Queue
).
(One approach might be to have your producer use threads, one thread to manage the dispatch from a threading Queue
to your multiprocessing.Queue
and any other threads actually just feeding the threading Queue
).
這篇關(guān)于多處理隊列最大大小限制為 32767的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!