基本上,当您使用多处理时,您的进程共享原始对象字典的副本,从而填充不同的字典。多处理包为您处理的是进程间python对象的消息传递,以使事情不那么痛苦。在
对于您的问题,一个好的设计是让主进程处理填充字典,并让它的子进程处理工作。然后使用队列在子进程和主进程之间交换数据。在
作为一个总体设计理念,以下是可以做到的:from queue import Queue
queues = [Queue(), Queue()]
def simulate(qin, qout):
while not qin.empty():
data = qin.pop()
# work with the data
qout.put(data)
# when the queue is empty, the process ends
Process(target=simulate, args=(queues[0][0],queues[0][1])).start()
Process(target=simulate, args=(queues[1][0],queues[1][1])).start()
processed_data_list = []
# first send the data to be processed to the children processes
while data.there_is_more_to_process():
# here you have to adapt to your context how you want to split the load between your processes
queues[0].push(data.pop_some_data())
queues[1].push(data.pop_some_data())
# then for each process' queue
for qin, qout in queues:
# you populate your output data list (or dict or whatever)
while not qout.empty:
processed_data_list.append(qout.pop())
# here again, you have to adapt to your context how you handle the data sent
# back from the children processes.
不过,这只是一个设计思想,因为这段代码有一些设计缺陷,在处理实际数据和处理函数时,这些缺陷自然会得到解决。在