如何从多个过程中收集结果?

时间:2016-01-03 13:30:23

标签: python multiprocessing

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

说清楚: 运行代码后:我得到了这个输出:

adView.setAdSize(AdSize.SMART_BANNER);

我想要的是:

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}

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

2 个答案:

答案 0 :(得分:6)

请勿使用全局,请使用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}

答案 1 :(得分:2)

您似乎错误地使用了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