如何在Python多处理中使用类? [英] How to use classes with Python multiprocessing?

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

问题描述

下面是一些示例代码,这些示例代码读取一个文件并加总每一行.应该将0-20之间的所有数字相加.但是,我总是得到0的结果.

Here's some sample code that is reads a file and adds up each line. It is supposed to add up all the numbers from 0-20. However, I always get a result of 0.

我看到中间计算成功了,为什么最终结果是0?

I can see that intermediate calculations are succeeding, so why is the final result 0?

是否有更好的方法可以做到这一点?我正在尝试在更大,更复杂的输入文件上进行更多计算,并随时存储一些统计信息.

Is there a better way to do this? I am trying to do more calcuations on a larger, more complex input file, and store some statistics as I go.

import multiprocessing
import StringIO

class Total():
    def __init__(self):
        self.total = 0

    def add(self, number):
        self.total += int(number)

    def __str__(self):
        return str(self.total)

total = Total()

def f(input):
    total.add(input)

# Create mock file
mock_file = StringIO.StringIO()
for i in range(20):
    mock_file.write("{}\n".format(i))
mock_file.seek(0)

# Compute
pool = multiprocessing.Pool(processes=4)
pool.map(f, mock_file)

print total

# Cleanup
mock_file.close()

推荐答案

您可以使用

You can accomplish this using shared memory with subprocess.Value, just change your Total class to the following:

class Total():
    def __init__(self):
        self.total = multiprocessing.Value('d', 0)

    def add(self, number):
        self.total.value += int(number)

    def __str__(self):
        return str(self.total.value)

这篇关于如何在Python多处理中使用类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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