如何从多进程中收集结果?

How to gather results from multiprocesses?

我的 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()

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

您似乎使用 global 不正确。它用于确保每当您提到 variable 时,您指的是全局范围内的那个:

#global scope
count = 0

def fun():
    #local variables
    local_count = 0

    # 'when I say "do something to `count`",
    # I mean the global variable'

    global count

    count += 1

你需要先声明这些变量,像这样:

from multiprocessing import Process, Lock, cpu_count

# initialize global variables

d = {}
lock = Lock()

class multiprocessingExample():

    global d
    # here you're overwriting them, so previous
    # values are no longer available.
    # you probably shouldn't do this, better initialize them
    # in global namespace

    #d = {}
    global lock

注意你也可以做global d, lock, something_else,这样就不用每次都写global

不要使用全局,使用 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}