Search code examples
pythonpicklepython-multiprocessing

AttributeError: Can't pickle local object 'SharedMemoryDisplay.__init__.<locals>.<lambda>'


I am working with this object SharedMemoryDisplay in the above script and I want to return/retrieve self.camera_container, where self.camera_container = {camera_id: (camera_id, frame, frame_properties)} I tried to create a method to return the this but it gives two errors

   prop, camera_container = monitor_memory.get_frame()
   TypeError: 'NoneType' object is not iterable
   AttributeError: Can't pickle local object 'SharedMemoryDisplay.__init__.<locals>.<lambda>'

I am only able to able to get self.camera_container[key] if I just do which is fine but I want to get self.camera_container also.

 return self.camera_container[key]

In the below script is where, I am using this object to display in a cv2 named window, my ultimate motive is to retrieve frames of all the cameras seperately what it currently does is joins all the camera frames and returns that via self.display_frame which is added to webdisplay_memory in the below script (for displaying in the html) that's why I created a method to retrieve dictionary camera_container. webdisplay_memory.add_frame(0, self.display_frame, None) rather than messing with this variable I was thinking of creating a method that returns self.camera_container than using this to get frames of each camera seperately.

how can I overcome this, kindly help if you have better and efficient solutions!


Solution

  • multiprocessing uses pickle under the hood. pickle can serialize only a certain set of objects. Specifically, it can not serialize defaultdict (a type for your camera_container). So, either use a normal dict and replace all the lookups to self.camera_container.get(key, None), or look into this question and try to use pathos.multiprocessing with dill. The latter approach is not tested by me, though.