![](/img/trans.png)
[英]Broken Pipe when Using Python Multiprocessing Managers (BaseManager/SyncManager) to Share Queue with Remote Machines
[英]Python, Using Remote Managers and Multiprocessing
我想使用multiprocessing
模塊中的遠程管理器功能在許多計算機之間分配工作。 我知道有第3方模塊,但我想盡可能地堅持使用核心。 我知道對於台式機(單機),您可以使用multiprocessing.Pool
類來限制CPU的數量,但是遠程管理器有一些問題。
我為遠程管理器提供以下代碼:
from multiprocessing.managers import BaseManager
import Queue
queue = Queue.Queue()
class QueueManager(BaseManager): pass
QueueManager.register('get_queue', callable=lambda:queue)
m = QueueManager(address=('', 50000), authkey='abracadabra')
s = m.get_server()
s.serve_forever()
這很好用,我什至可以使用以下代碼將作業提交到隊列中:
QueueManager.register('get_queue')
m = QueueManager(address=('machinename', 50000), authkey='abracadabra')
m.connect()
queue = m.get_queue()
queue.put('hello')
您還可以queue.get()
獲取隊列中的單個條目。
遍歷隊列與執行操作相同:
while True:
elem = queue.get() #queue empty -> it blocks!!!
在沒有更多要執行的作業時 “迭代”隊列並阻塞您的工作進程的一種優雅方法是將None
(或其他方式)用作哨兵並使用iter(callable, sentinel)
:
for job in iter(queue.get, None):
# execute the calculation
output_queue.put(result)
#shutdown the worker process
等效於:
while True:
job = queue.get()
if job is None:
break
#execute the calculation
output_queue.put(result)
#shutdown the worker process
請注意,您必須在隊列中為每個輔助子進程插入一個前哨,否則將有子進程在等待它。
關於你的第二個問題,我不明白你在問什么。 BaseManager
提供了一個服務器,該服務器執行來自客戶端的調用,因此,顯然,所有請求都由同一台計算機滿足。 還是說讓每個客戶只做一個請求? 我看不到有任何選擇,盡管可以“手動”實現。
我不明白你的問題。 什么樣的拉法? 您能否以“可以觸發多處理服務器的推送方法”的含義來詳細說明一下您的問題?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.