python 元类修改后的类无法进行持久化?

python 元类修改后的类无法进行持久化?

问题可能涉及的知识:

python 元类
python 数据持久化(pickle)
python 多线程(不一定)

我在进行python多进程操作时出现了问题
部分代码:

from multiprocessing import Process, Queue

from queue import Empty

def init(cls):

def __init__(self, *args, **kwargs):

queue = Queue()

Process(target=process_main, args=(cls, queue, args, kwargs), daemon=True).start()

map_fun_ = map_fun(queue)

for i in dir(cls):

if i not in not_transfer_fun and i[:2] == i[-2:] == '__' and callable(getattr(cls, i)):

setattr(self, i, map_fun_(i))

return __init__

class ProcessClassMeta(type):

def __new__(cls, name, bases, attrs):

self_cls = type(name, bases, attrs)

cls = type(name, (object,), {'__init__': init(self_cls)})

return cls

测试部分代码:

from os import getpid

class B:

def __init__(self):

print(getpid())

self.c = 'c'

def a(self):

print(getpid())

self.c = 'a'

class A(B, metaclass=ProcessClassMeta):

def b(self):

print(getpid())

self.c = 'b'

def a(self):

print(getpid())

self.c = 'd'

print(getpid())

a = A()

print(a.c)

a.b()

print(a.c)

a.a()

print(a.c)

输出:

5780

Traceback (most recent call last):

File "D:/1/project/close2/ProcessClass.py", line 231, in <module>

a = A()

File "D:/1/project/close2/ProcessClass.py", line 190, in __init__

Process(target=process_main, args=(cls, queue, args, kwargs), daemon=True).start()

File "D:\1\python\WPy32-3770\python-3.7.7\lib\multiprocessing\process.py", line 112, in start

self._popen = self._Popen(self)

File "D:\1\python\WPy32-3770\python-3.7.7\lib\multiprocessing\context.py", line 223, in _Popen

return _default_context.get_context().Process._Popen(process_obj)

File "D:\1\python\WPy32-3770\python-3.7.7\lib\multiprocessing\context.py", line 322, in _Popen

return Popen(process_obj)

File "D:\1\python\WPy32-3770\python-3.7.7\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__

reduction.dump(process_obj, to_child)

File "D:\1\python\WPy32-3770\python-3.7.7\lib\multiprocessing\reduction.py", line 60, in dump

ForkingPickler(file, protocol).dump(obj)

_pickle.PicklingError: Can't pickle <class '__main__.A'>: it's not the same object as __main__.A

本代码是为了实现简化多进程操作,让多进程像单进程一样操作
我在元类中用type创建了一个类来替换原来的,于是就无法继续持久化用于传参了


回答:

from multiprocessing import Process, Manager

from os import getpid

def process_main(cls, dic, args, kwargs):

instance = cls(*args, **kwargs)

for attr in dir(cls):

if attr[:2] == attr[-2:] == '__' and callable(getattr(cls, attr)):

dic[attr] = getattr(instance, attr)

class B:

def __init__(self):

print(getpid())

self.c = 'c'

def a(self):

print(getpid())

self.c = 'a'

class A(B):

def __init__(self, *args, **kwargs):

self.manager = Manager()

self.dic = self.manager.dict()

Process(target=process_main, args=(A, self.dic, args, kwargs), daemon=True).start()

def b(self):

print(getpid())

self.dic['c'] = 'b'

def a(self):

print(getpid())

self.dic['c'] = 'd'

@property

def c(self):

return self.dic.get('c')

print(getpid())

a = A()

print(a.c)

a.b()

print(a.c)

a.a()

print(a.c)

以上是 python 元类修改后的类无法进行持久化? 的全部内容, 来源链接: utcz.com/p/938905.html

回到顶部