使用多重处理模块

编程入门 行业动态 更新时间:2024-10-13 10:28:26
本文介绍了使用多重处理模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在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.

更多推荐

使用多重处理模块

本文发布于:2023-10-28 04:40:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!