使用多重处理模块 [英] Using the multiprocessing module

查看:53
本文介绍了使用多重处理模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python 2.6中使用多处理模块,但是显然有些我不了解的东西.我希望下面的类将add()发送给它的数字加起来,并在get_result()方法中返回总和.下面的代码打印"0",我希望它打印"2".我错过了什么?

I'm trying to use the multiprocessing module in python 2.6, but apparently there is something I do not understand. I would expect the class below to add up the numbers sent to it by add() and return the sum in the get_result() method. The code below prints "0", I'd like it to print "2". What have I missed?

import multiprocessing

class AdderProcess(multiprocessing.Process):

    def __init__(self):
        multiprocessing.Process.__init__(self)
        self.sum = 0
        self.queue = multiprocessing.JoinableQueue(5)
        self.daemon = True
        self.start()

    def run(self):
        while True:
            number = self.queue.get()
            self.sum += number
            self.queue.task_done()

    def add(self, number):
        self.queue.put(number)

    def get_result(self):
        self.queue.join()
        return self.sum


p = AdderProcess()
p.add(1)
p.add(1)
print p.get_result()

PS.此问题已解决.谢谢您的回答!为了使所有读者更轻松,这是完整的工作版本:

PS. This problem has been solved. Thanks for the answers! Just to make it easier for any readers, here's the complete working version:

import multiprocessing

class AdderProcess(multiprocessing.Process):

    def __init__(self):
        multiprocessing.Process.__init__(self)
        self.sum = multiprocessing.Value('d', 0.0)
        self.queue = multiprocessing.JoinableQueue(5)
        self.daemon = True
        self.start()

    def run(self):
        while True:
            number = self.queue.get()
            self.sum.value += number
            self.queue.task_done()

    def add(self, number):
        self.queue.put(number)

    def get_result(self):
        self.queue.join()
        return self.sum.value

p = AdderProcess()
p.add(1)
p.add(1)
print p.get_result()

推荐答案

self.sum = 0更改为self.sum = multiprocessing.Value('d', 0.0),然后使用self.sum.value访问或更改值.

Change self.sum = 0 to self.sum = multiprocessing.Value('d', 0.0), and use self.sum.value to access or change the value.

class AdderProcess(multiprocessing.Process):    
    def __init__(self):
        ...
        self.sum = multiprocessing.Value('d', 0.0) 
        ...
    def run(self):
        while True:
            number = self.queue.get()
            self.sum.value += number    # <-- use self.sum.value
            self.queue.task_done()
    def get_result(self):
        self.queue.join()
        return self.sum.value           # <-- use self.sum.value

问题是这样的:一旦在__init__中调用self.start(),主进程就会派生一个子进程.所有值都将被复制.现在有p的两个版本.在主进程中,p.sum为0.在子进程中,run方法被调用,而p.sum被扩展为2.但是当主进程调用p.get_result()时,其p的版本仍然具有p.sum等于0. 因此,将打印0.

The problem is this: Once you call self.start() in __init__, the main process forks off a child process. All values are copied. Now there are two versions of p. In the main process, p.sum is 0. In the child process, the run method is called and p.sum is augmented to 2. But when the main process calls p.get_result(), its version of p still has p.sum equal to 0. So 0 is printed.

要在进程之间共享浮点值时,需要使用共享机制,例如mp.Value.

When you want to share a float value between processes, you need to use a sharing mechanism, such as mp.Value.

请参阅"进程之间的共享状态"有关如何分享价值的更多选择.

See "Sharing state between processes" for more options on how to share values.

这篇关于使用多重处理模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆