[英]python shared Queue() in shared dict() in multiprocessing
我剛剛學習了python多處理。 我想建立一個模型來模擬網絡中發送和接收消息的過程。 有向圖描述了兩個節點之間的關系,而字典描述了兩個節點之間的通信。 該詞典的值的數據類型為queue。 但是我遇到了一些錯誤:
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import Manager
PoolGroup=[('R1','R2','R3'),('N1','N2','N3'),('E1','E2','E3')]
PoolElement=['R1','R2','R3','N1','N2','N3','E1','E2','E3']
graph={'R1':['N1','N2','N3'],
'R2':['N1','N2','N3'],
'R3':['N1','N2','N3'],
'N1':['E1','E2','E3'],
'N2':['E1','E2','E3'],
'N3':['E1','E2','E3'],
'E1':[],
'E2':[],
'E3':[]}
def addSigal(target,information):
AllQueue[target].put(information)
print("Succeed in sending msg to "+target)
print(target+' now has ',AllQueue[target].qsize(),' signals')
def pool1function(name,information):
targetlist=list(graph[name])
print(name+" send information to "+str(targetlist))
with ProcessPoolExecutor() as pool1:
pool1.map(addSigal,targetlist,[information]*3)
if __name__=='__main__':
m=Manager()
AllQueue=m.dict()
AllQueue.update({PE:m.Queue() for PE in PoolElement})
with ProcessPoolExecutor() as pool:
pool.map(pool1function,PoolGroup[0],[1,2,3])
不幸的是,結果僅顯示:
R1 send information to ['N1', 'N2', 'N3']
R2 send information to ['N1', 'N2', 'N3']
R3 send information to ['N1', 'N2', 'N3']
這意味着信息不會發送到相應的節點。 所以我檢查了AllQueue並發現了一些奇怪的東西:當我打印AllQueue ['R1']時,它顯示:
RemoteError:
---------------------------------------------------------------------------
Unserializable message: ('#RETURN', <queue.Queue object at 0x10edd8dd8>)
---------------------------------------------------------------------------
我也無法從AllQueue ['R1']放置元素或獲取元素,這是什么問題?
這是將字典傳遞給任務的示例:
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import Manager
def addSigal(target, information, q):
print(target,information,q)
q[target]=(information)
print("Succeed in sending msg to "+target)
print(target+' now has ',q[target])
if __name__=='__main__':
m = Manager()
AllQueue = m.dict()
AllQueue.update({'A':0,'B':1})
with ProcessPoolExecutor() as pool:
pool.map(addSigal,'AB', [1, 2],[AllQueue,AllQueue])
print(AllQueue)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.