gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:31 25 4
gpt4 key购买 nike

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

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

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

return Wrapper

我需要通过 multiprocessing.Queue 与使用 multiprocessing 模块生成的进程共享 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: http://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 的评论。

关于python - 工厂方法模式与多处理队列的使用冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24657781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com