简体   繁体   English

多处理中共享dict()中的python shared Queue()

[英]python shared Queue() in shared dict() in multiprocessing

I just learnt python multiprocessing. 我刚刚学习了python多处理。 I want to make a model to simulate the process of sending and receiving messages in a networks. 我想建立一个模型来模拟网络中发送和接收消息的过程。 A directed graph describes the relation between two nodes and a dictionary describes the communication between two nodes. 有向图描述了两个节点之间的关系,而字典描述了两个节点之间的通信。 The data type of the value of this dictionary is queue. 该词典的值的数据类型为queue。 But I met some errors: 但是我遇到了一些错误:

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])

Unfortunately, the result just showed: 不幸的是,结果仅显示:

R1 send information to ['N1', 'N2', 'N3']
R2 send information to ['N1', 'N2', 'N3']
R3 send information to ['N1', 'N2', 'N3']

it means the information is not sent to the corresponding node. 这意味着信息不会发送到相应的节点。 So I checked AllQueue and found something strange: when I print AllQueue['R1'], it showed: 所以我检查了AllQueue并发现了一些奇怪的东西:当我打印AllQueue ['R1']时,它显示:

RemoteError: 
---------------------------------------------------------------------------
Unserializable message: ('#RETURN', <queue.Queue object at 0x10edd8dd8>)
---------------------------------------------------------------------------

I also failed to put or get element from AllQueue['R1'], what's the problem? 我也无法从AllQueue ['R1']放置元素或获取元素,这是什么问题?

This is an example of passing the dictionary to a task: 这是将字典传递给任务的示例:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM