Search code examples
pythonpython-2.7rospy

Multithreading Multiprocessing with non serialisable classes


I have a python class which communicates via a ROS service with a simulation environment. Now I want to communicate with multiple simulations in parallel.

In detail, the environment class has a step function which takes an argument and returns the simulation observations:

self.envs = [Environment("AI1"),
             Environment("AI2")]
self.pool = Pool(processes=len(self.envs))

ac = # some actions 
res0 = self.pool.apply_async(self.envs[0].step, ac[0])
res1 = self.pool.apply_async(self.envs[1].step, ac[1])

res_list = []
res_list.extend(res0.get())
res_list.extend(res1.get())

Of course, I get now the following error

cPickle.PicklingError: Can't pickle : attribute lookup builtin.instancemethod failed

because a ROSPy service is a member of Environment

Is it possible to create the environments in the pool itself? Or would switching to python threading help?


Solution

  • With threads you won't get this issue.

    An alternative is to start subprocesses where the subprocess creates the Environment and listens on a queue for requests and pushes responses onto a different queue. That way the environment lives in the subprocess.