python – 工厂方法模式与使用多处理队列冲突

我已经实现了工厂方法模式来参数化产品类的基类:

def factory(ParentClass):
    class Wrapper(ParentClass):
        _attr = "foo"

        def wrapped_method():
            "Do things to be done in `ParentClass`."""
            return _attr

    return Wrapper

我需要通过multiprocessing.Queue与使用多处理模块生成的进程共享Wrapper对象.

由于multiprocessing.Queue使用Pickle存储对象(请参阅Pickle documentation处的注释),并且未在顶层定义Wrapper,因此出现以下错误:

PicklingError: Can't pickle <class 'Wrapper'>: attribute lookup Wrapper failed

我使用了这个answer中的解决方法,我得到另一个错误:

AttributeError: ("type object 'ParentClass' has no attribute 'Wrapper'", <main._NestedClassGetter object at 0x8c7fe4c>, (<class 'ParentClass'>, 'Wrapper'))

是否有解决方案在进程间共享这些对象?

最佳答案 根据
Pickle documentation,问题中链接的变通方法可以修改为:

class _NestedClassGetter(object):
    """
    From: https://stackoverflow.com/a/11493777/741316
    When called with the containing class as the first argument,
    and the name of the nested class as the second argument,
    returns an instance of the nested class.
    """
    def __call__(self, factory_method, base):
        nested_class = factory_method(base)

        # make an instance of a simple object (this one will do), for which we
        # can change the __class__ later on.
        nested_instance = _NestedClassGetter()

        # set the class of the instance, the __init__ will never be called on
        # the class but the original state will be set later on by pickle.
        nested_instance.__class__ = nested_class
        return nested_instance

和__reduce__方法:

    def __reduce__(self):
        state = self.__dict__.copy()
        return (_NestedClassGetter(),
                (factory, ParentClass), state,)

感谢@dano的评论.

点赞