我有以下类:
class MDP(object):
def __init__(self, level, state_var):
self.state_var = state_var
self.level = level
...
def __repr__(self):
return "level {} var {}".format(self.level, self.state_var)
def __eq__(self, other):
return self.level == other.level and self.state_var == other.state_var
def __hash__(self):
return hash((self.level,) + self.state_var)
def __lt__(self, other):
return self.state_var < other.state_var
...
我有另一个类为我的GUI看起来像这样:
class GUI:
...
self.queue = multiprocessing.Queue()
self.process = multiprocessing.Process(target=self.start, args=(self.queue,))
self.process.start()
def start(self, queue):
...
pygame.init()
...
while self.run:
clock.tick(Consts.FPS)
if not queue.empty():
event = queue.get()
self.container.render()
pygame.display.update()
queue.close()
pygame.quit()
sys.exit()
def render_q_values(self, q_values):
self.queue.put(Event(EventType.QVAL, q_values))
事件是简单的,看起来像这样:
class Event():
def __init__(self, kind, data):
self.kind = kind
self.data = data
当我调用gui.render_q_values(q_values)
时,q_values
是一个字典,其中键是MDP
对象,值是整数,我在event = queue.get()
行上得到以下错误:
event = queue.get()
File "/.../python3.6/multiprocessing/queues.py", line 113, in get
return _ForkingPickler.loads(res)
File ".../mdp.py", line 50, in __eq__
return self.level == other.level and self.state_var == other.state_var
AttributeError: 'MDP' object has no attribute 'level'
我的MDP对象是可哈希的,所有这些属性都是初始化的,我把它们放在集合和字典中,但是当我尝试使用多处理模块将它们放在队列中时,我得到了这些错误。主要问题似乎是多处理模块不能pickle我的MDP对象,知道为什么吗?
此错误实际上与多处理模块无关,而与我在MDP对象中定义相等和散列函数的方式有关:Can't pickle具有递归引用的对象