gpt4 book ai didi

python - 如何从多处理中收集结果?

转载 作者:太空狗 更新时间:2023-10-30 00:49:03 25 4
gpt4 key购买 nike

我的 python 代码有问题。我想要的是每个进程都写在一本字典中。我得到的是每个进程都写入自己的字典。

明确一点:运行代码后:我得到这个输出:

P 0: {0: 1}
P 2: {2: 1}
P 4: {4: 1}
P 6: {6: 1}
P 8: {8: 1}
All: {}

我想要的是:

P 0: {0: 1}
P 2: {2: 1}
P 4: {4: 1}
P 6: {6: 1}
P 8: {8: 1}
All: {0: 1, 2: 1, 4: 1, 6: 1, 8: 1}

这是我的示例代码:

from multiprocessing import Process, Lock, cpu_count

class multiprocessingExample():

global d
d = {}
global lock
lock = Lock()

def __init__(self):
pass

def proc(self, num):

global lock
global d
with lock:
if(num in d):
d[num] = d[num] + 1
else:
d[num] = 1
print("P " + str(num) + ": " + str(d))

def main(self):
jobs = []

for i in range(0, 10):
if(i%2 == 0):
p = Process(target=self.proc, args=(i,))
jobs.append(p)

for job in jobs:
job.start()

for job in jobs:
job.join()

print("All: " + str(d))

obj = multiprocessingExample()
obj.main()

如果你能告诉我哪里出了问题,那就太好了。

最佳答案

不要使用全局,使用 Manager.dict :

from multiprocessing import Process, Lock, Manager


class multiprocessingExample():
def __init__(self):
self.m = Manager()
self.d = self.m.dict()
self.lock = Lock()

def proc(self, num):
with self.lock:
if (num in self.d):
self.d[num] = d[num] + 1
else:
self.d[num] = 1
print("P " + str(num) + ": " + str(self.d))
def main(self):
jobs = []
for i in range(0, 10):
if (i % 2 == 0):
p = Process(target=self.proc, args=(i,))
jobs.append(p)

for job in jobs:
job.start()
for job in jobs:
job.join()
print("All: " + str(self.d))


obj = multiprocessingExample()
obj.main()

这将输出如下内容:

P 0: {0: 1}
P 2: {0: 1, 2: 1}
P 4: {0: 1, 2: 1, 4: 1}
P 8: {0: 1, 8: 1, 2: 1, 4: 1}
P 6: {0: 1, 8: 1, 2: 1, 4: 1, 6: 1}
All: {0: 1, 8: 1, 2: 1, 4: 1, 6: 1}

关于python - 如何从多处理中收集结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34577047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com