当前位置: 首页 > 面试题库 >

为什么我可以将实例方法传递给multiprocessing.Process,而不是multiprocessing.Pool?

章嘉致
2023-03-14
问题内容

我正在尝试编写一个与并发应用功能的应用程序multiprocessing.Pool。我希望这个函数是一个实例方法(因此我可以在不同的子类中不同地定义它)。这似乎是不可能的。正如我在其他地方了解到的那样,显然不能Pickling绑定方法。那么,为什么multiprocessing.Process以绑定方法作为目标开始?如下代码:

import multiprocessing

def test1():
    print "Hello, world 1"

def increment(x):
    return x + 1

class testClass():
    def process(self):
        process1 = multiprocessing.Process(target=test1)
        process1.start()
        process1.join()
        process2 = multiprocessing.Process(target=self.test2)
        process2.start()
        process2.join()

    def pool(self):
        pool = multiprocessing.Pool(1)
        for answer in pool.imap(increment, range(10)):
            print answer
        print
        for answer in pool.imap(self.square, range(10)):
            print answer

    def test2(self):
        print "Hello, world 2"

    def square(self, x):
        return x * x

def main():
    c = testClass()
    c.process()
    c.pool()

if __name__ == "__main__":
    main()

产生以下输出:

Hello, world 1
Hello, world 2
1
2
3
4
5
6
7
8
9
10

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Python27\Lib\threading.py", line 551, in __bootstrap_inner
    self.run()
  File "C:\Python27\Lib\threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python27\Lib\multiprocessing\pool.py", line 319, in _handle_tasks
    put(task)
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

为什么进程可以处理绑定方法,但不能处理池?


问题答案:

pickle模块通常不能腌制实例方法:

>>> import pickle
>>> class A(object):
...  def z(self): print "hi"
... 
>>> a = A()
>>> pickle.dumps(a.z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "/usr/local/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/usr/local/lib/python2.7/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/usr/local/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects

但是,该multiprocessing模块具有一个自定义Pickler,该自定义添加了一些代码以启用此功能:

#
# Try making some callable types picklable
#

from pickle import Pickler
class ForkingPickler(Pickler):
    dispatch = Pickler.dispatch.copy()

    @classmethod
    def register(cls, type, reduce):
        def dispatcher(self, obj):
            rv = reduce(obj)
            self.save_reduce(obj=obj, *rv)
        cls.dispatch[type] = dispatcher

def _reduce_method(m):
    if m.im_self is None:
        return getattr, (m.im_class, m.im_func.func_name)
    else:
        return getattr, (m.im_self, m.im_func.func_name)
ForkingPickler.register(type(ForkingPickler.save), _reduce_method)

您可以使用copy_reg模块复制此文件,以查看它是否可以自己运行:

>>> import copy_reg
>>> def _reduce_method(m):
...     if m.im_self is None:
...         return getattr, (m.im_class, m.im_func.func_name)
...     else:
...         return getattr, (m.im_self, m.im_func.func_name)
... 
>>> copy_reg.pickle(type(a.z), _reduce_method)
>>> pickle.dumps(a.z)
"c__builtin__\ngetattr\np0\n(ccopy_reg\n_reconstructor\np1\n(c__main__\nA\np2\nc__builtin__\nobject\np3\nNtp4\nRp5\nS'z'\np6\ntp7\nRp8\n."

当您用来Process.start在Windows上生成新进程时,它会使用以下自定义来腌制传递给子进程的所有参数ForkingPickler

#
# Windows
#

else:
    # snip...
    from pickle import load, HIGHEST_PROTOCOL

    def dump(obj, file, protocol=None):
        ForkingPickler(file, protocol).dump(obj)

    #
    # We define a Popen class similar to the one from subprocess, but
    # whose constructor takes a process object as its argument.
    #

    class Popen(object):
        '''
        Start a subprocess to run the code of a process object
        '''
        _tls = thread._local()

        def __init__(self, process_obj):
            # create pipe for communication with child
            rfd, wfd = os.pipe()

            # get handle for read end of the pipe and make it inheritable
            ...
            # start process
            ...

            # set attributes of self
            ...

            # send information to child
            prep_data = get_preparation_data(process_obj._name)
            to_child = os.fdopen(wfd, 'wb')
            Popen._tls.process_handle = int(hp)
            try:
                dump(prep_data, to_child, HIGHEST_PROTOCOL)
                dump(process_obj, to_child, HIGHEST_PROTOCOL)
            finally:
                del Popen._tls.process_handle
                to_child.close()

请注意“发送信息给孩子”部分。它使用的dump功能是ForkingPickler用来腌制数据,这意味着您的实例方法可以被腌制。

现在,使用on方法multiprocessing.Pool将方法发送给子进程时,它是使用amultiprocessing.Pipe来腌制数据。在python
2.7中,它multiprocessing.Pipe是用C实现的,并且pickle_dumps直接调用,因此它没有利用ForkingPickler。这意味着腌制实例方法不起作用。

但是,如果您使用类型而不是customcopy_reg来注册instancemethod类型Pickler,则 所有
对酸洗的尝试都会受到影响。因此,您甚至可以通过Pool以下方式使用它来启用酸洗实例方法:

import multiprocessing
import copy_reg
import types

def _reduce_method(m):
    if m.im_self is None:
        return getattr, (m.im_class, m.im_func.func_name)
    else:
        return getattr, (m.im_self, m.im_func.func_name)
copy_reg.pickle(types.MethodType, _reduce_method)

def test1():
    print("Hello, world 1")

def increment(x):
    return x + 1

class testClass():
    def process(self):
        process1 = multiprocessing.Process(target=test1)
        process1.start()
        process1.join()
        process2 = multiprocessing.Process(target=self.test2)
        process2.start()
        process2.join()

    def pool(self):
        pool = multiprocessing.Pool(1)
        for answer in pool.imap(increment, range(10)):
            print(answer)
        print
        for answer in pool.imap(self.square, range(10)):
            print(answer)

    def test2(self):
        print("Hello, world 2")

    def square(self, x):
        return x * x

def main():
    c = testClass()
    c.process()
    c.pool()

if __name__ == "__main__":
    main()

输出:

Hello, world 1
Hello, world 2
GOT (0, 0, (True, 1))
GOT (0, 1, (True, 2))
GOT (0, 2, (True, 3))
GOT (0, 3, (True, 4))
GOT (0, 4, (True, 5))
 1GOT (0, 5, (True, 6))

GOT (0, 6, (True, 7))
2
GOT (0, 7, (True, 8))
3
 GOT (0, 8, (True, 9))
GOT (0, 9, (True, 10))
4
5
6
7
8
9
10

GOT (1, 0, (True, 0))
0
GOT (1, 1, (True, 1))
1
GOT (1, 2, (True, 4))
4
GOT (1, 3, (True, 9))
9
 GOT (1, 4, (True, 16))
16
GOT (1, 5, (True, 25))
25
 GOT (1, 6, (True, 36))
36
 GOT (1, 7, (True, 49))
49
 GOT (1, 8, (True, 64))
64
GOT (1, 9, (True, 81))
81
GOT None

还要注意,在Python 3.x中,pickle可以本地腌制实例方法类型,因此这些东西都不再重要了。:)



 类似资料:
  • 问题内容: 为什么经常被称为 代替 ? W3,MDN和MSDN都声明它是可选的。此外,ActiveX控件似乎不需要参数: 这种做法至少可以追溯到2005年的Google Maps中 ,但被缩小了,没有任何解释: 问题答案: 如果您看一下XMLHttpRequest的旧规范,似乎W3C似乎并不需要在某一点上将该参数设为可选,这可能导致人们提供了一个明确的null值,以防万一。 (搜索“应支持发送”)

  • 为什么第一个和第二个写工作,但不是最后一个?有没有办法我可以允许所有3个,并检测它是1,(int)1还是i传入?为什么只允许一个,而允许最后一个?第二个被允许,但不是最后一个,真的让我大吃一惊。 演示显示编译错误

  • 问题内容: 我的问题是: 没有这一行,我什么也没插入? 为什么不复制到新的临时文件夹,我什么也不会复制? 问题答案: 正如另一个答案所说,这样做: 在列表中添加对同一对象的引用。实际上,这里发生的是: 指清单 该引用作为参数传递给 对列表的引用存储在内部 但是请注意,两者和内部的引用都指向 同一个对象 当您执行此操作时,会发生以下情况: 创建一个新列表,并用引用原始列表中所有数据的副本。 此列表已

  • 问题内容: 我试图将一个组件的引用传递给另一个组件。由于不推荐使用字符串引用,因此我使用了回调引用。 所以我有类似的东西: 问题是,每当我尝试访问内部时,我都会得到。 我什至尝试过: 似乎问题在于,当创建prop时,ref并不存在,因为它是在安装后创建的。但是我不知道如何“刷新”道具以获取对已安装组件的参考。 那么将ref传递到另一个组件的正确方法是什么? 编辑 一些用户建议将该逻辑封装在一个更高

  • 问题内容: 目前,我在隐藏的输入字段中回显某些变量,并在需要时使用Javascript读取它们。 我和一个同事现在正在考虑使用PHP生成一个额外的Javascript文件,该文件仅包含Javascript的所有变量。这样,变量已经存在,HTML中没有多余的代码。 有什么好的方法可以将变量从PHP传递到Javascript?我们的解决方案听起来如何? 问题答案: 通用数据传递 JavaScript常

  • 问题内容: 我定义了一个Java函数: 一种调用方式是这样的: 为什么不能通过显式传递泛型类型参数来调用它?: 我从编译器得到错误。 问题答案: 当Java编译器无法自行推断静态方法的参数类型时,您始终可以使用完整的合格方法名称Class来传递它。<类型> method();