在绑定方法上使用multiprocessing.Pool

时间:2014-03-17 08:59:46

标签: python python-2.7 multiprocessing pickle pool

我正在尝试在代码中使用multiprocessing.Pool,但我遇到了这个例外:

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

我找到了this,而preferred solution recipe

我的问题是我不知道如何在我的代码中实现此解决方案。

我的代码是这样的:

class G(class):
    def submit(self,data):
        cmd = self.createCommand(data)
        subprocess.call(cmd, shell=True)
        # call for a short command

    def main(self):
        self.pool = multiprocessing.Pool()
        while(True):
            data = self.GenerateData()
            self.pool.apply_async(self.Submit, args=(data,))

一些注意事项:

  • while应该可以使用很长时间(几天)
  • 我正在使用pool用于性能目的,如果您有更好的解决方案,我会很高兴来到这里

更新

使用@unutbu解决方案后,我得到了下一个异常: PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed

现在,我发现的所有解决方案都在谈论Queue.Queuemp.Pool.map,但我没有使用这些属性,所以我无法弄明白。

1 个答案:

答案 0 :(得分:0)

这是Steven Bethard的解决方案适用于您的情况:

import multiprocessing as mp
import time
import copy_reg
import types

def _pickle_method(method):
    """
    Author: Steven Bethard 
    http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods
    """
    func_name = method.im_func.__name__
    obj = method.im_self
    cls = method.im_class
    cls_name = ''
    if func_name.startswith('__') and not func_name.endswith('__'):
        cls_name = cls.__name__.lstrip('_')
    if cls_name:
        func_name = '_' + cls_name + func_name
    return _unpickle_method, (func_name, obj, cls)


def _unpickle_method(func_name, obj, cls):
    """
    Author: Steven Bethard
    http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods
    """
    for cls in cls.mro():
        try:
            func = cls.__dict__[func_name]
        except KeyError:
            pass
        else:
            break
    return func.__get__(obj, cls)

# This call to copy_reg.pickle allows you to pass methods as the first arg to
# mp.Pool methods. If you comment out this line, `pool.map(self.foo, ...)` results in
# PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup
# __builtin__.instancemethod failed

copy_reg.pickle(types.MethodType, _pickle_method, _unpickle_method)

class G(object):
    def submit(self, data):
        print('processing {}'.format(data))
        # cmd = self.createCommand(data)
        # subprocess.call(cmd, shell=True)
        # call for a short command
        time.sleep(2)

    def main(self):
        pool = mp.Pool()
        while True:
            data = (1, 2, 3)
            pool.apply_async(self.submit, args=(data,))

if __name__ == '__main__':
    g = G()
    g.main()