python语言p语言_用p语言编写对象词典

基本上,当您使用多处理时,您的进程共享原始对象字典的副本,从而填充不同的字典。多处理包为您处理的是进程间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.

不过,这只是一个设计思想,因为这段代码有一些设计缺陷,在处理实际数据和处理函数时,这些缺陷自然会得到解决。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值