简体   繁体   中英

Python multiprocessing Queues pickling error

I have the following class:

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
    ...

I have another class for my GUI that looks like this:

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))

Event is simple and looks like this:

class Event():
    def __init__(self, kind, data):
        self.kind = kind
        self.data = data

When I call gui.render_q_values(q_values) where q_values is a dictionary where keys are MDP objects and values are integers, I get the following error on the line 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'

My MDP objects are hashable and all those attributes are initialized, I have them in sets and dictionaries, but when I try to use the multiprocessing module to put them in Queues, I get these kind of errors. The main problem seems to be that the multiprocessing module can't pickle my MDP objects, any ideas why?

This error actually had nothing to do with the multiprocessing module, and all to do with how I defined equality and hash functions in my MDP object: Can't pickle objects with recursive references

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM