简体   繁体   English

当使用实例方法和函数进行多处理时,为什么会得到不同的结果?

[英]Why do I get different results when using multiprocessing with an instance method vs with a function?

For the following code, which passes an instance method to the Pool , the list is empty at the end of the script: 对于将实例方法传递给Pool的以下代码,该脚本末尾的列表为空:

import time

from multiprocessing import Pool

class Hello:
    def __init__(self):
        self.result_list=[]

    def f(self,x,y):
        time.sleep(2)
        return x*y


    def log_result(self,result):
        # This is called whenever foo_pool(i) returns a result.
        # result_list is modified only by the main process, not the pool workers.
        print result
        self.result_list.append(result)

if __name__ == '__main__':
    pool = Pool()              # start 4 worker processes
    h=Hello()
    for i in range(10):
        pool.apply_async(h.f, args = (i,i, ), callback = h.log_result)
    pool.close()
    pool.join()
    print(h.result_list)

With this code, the list is populated as expected. 使用此代码,列表将按预期填充。

import multiprocessing as mp
import time

def foo_pool(x):
    time.sleep(2)
    return x*x

result_list = []
def log_result(result):
    # This is called whenever foo_pool(i) returns a result.
    # result_list is modified only by the main process, not the pool workers.
    result_list.append(result)

def apply_async_with_callback():
    pool = mp.Pool()
    for i in range(10):
        pool.apply_async(foo_pool, args = (i, ), callback = log_result)
    pool.close()
    pool.join()
    print(result_list)

if __name__ == '__main__':
    apply_async_with_callback()

What's different about the two? 两者有什么不同? Why doesn't it work with the instance method? 为什么它不适用于实例方法?

If you actually try to fetch the result of one of you apply_async calls, you'll see that they're all failing with this error: 如果您实际上尝试获取其中一个apply_async调用的结果,您将看到它们都失败并出现此错误:

cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

This is because in Python 2.x, instance methods aren't picklable by default, so trying to pass the instance method hf to the worker process fails. 这是因为在Python 2.x中,默认情况下实例方法不可选,因此尝试将实例方法hf传递给工作进程失败。 This is actually fixed in Python 3, but you can backport the behavior to Python 2 quite easily, using the copy_reg module: 这实际上已在Python 3中修复,但您可以使用copy_reg模块将行为很容易地copy_reg移植到Python 2:

import time

from multiprocessing import Pool
import copy_reg
import types

def _reduce_method(m):
    if m.__self__ is None:
        return getattr, (m.__class__, m.__func__.__name__)
    else:
        return getattr, (m.__self__, m.__func__.__name__)

copy_reg.pickle(types.MethodType, _reduce_method)

class Hello:
    def __init__(self):
        self.result_list=[]

    def f(self,x,y):
        time.sleep(2)
        return x*y


    def log_result(self, result):
        print(result)
        self.result_list.append(result)

if __name__ == '__main__':
    pool = Pool()
    h = Hello()
    for i in range(10):
        pool.apply_async(h.f, args = (i,i), callback=h.log_result)
    pool.close()
    pool.join()
    print(h.result_list)

Output: 输出:

0
4
49
25
1
16
36
9
64
81
[0, 4, 49, 25, 1, 16, 36, 9, 64, 81]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 多处理和共享 memory - 为什么我得到不同的结果? - Python multiprocessing and shared memory - why do I get different results? 使用多重处理与多重处理时的结果不同 - Different results when using multiprocessing vs multiprocessing.dummy 为什么在手动拆分测试和训练数据而不是使用Python拆分功能时会得到不同的结果 - Why do I get different results when I do a manual split of test and train data as opposed to using the Python splitting function 如果我在长度为1的QuerySet上使用first()vs last(),为什么会得到不同的结果 - Why do I get different results if I use first() vs last() on a QuerySet with the length of 1 如何获得多处理结果? - How do I get multiprocessing results? 为什么当CURL与使用URLLIB2时,我会得到不同的结果? - How come when I CURL vs using URLLIB2, I get different results? 将损失乘以标量后,为什么在训练Keras模型时得到不同的结果? - Why do I get different results when training a Keras model when I multiply the loss by a scalar? 为什么从不同的文件位置调用时会得到不同的结果? - Why do I get different results when calling from different file locations? 当分别使用MATLAB或Python将数据导出到* .vtk文件时,为什么会得到不同的结果? - Why do I get different results, when I export data to *.vtk-files with MATLAB or Python, respectively? 为什么我在 pycharm 和 IDLE 中运行相同的程序会得到不同的结果? - Why do I get different results when I run the same program in pycharm and IDLE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM