Add pickle and unpickle support.#75
Add pickle and unpickle support.#75mo-vic wants to merge 2 commits intoBerkeleyAutomation:masterfrom
Conversation
1. fcl.Transform 2. fcl.CollisionObject 3. fcl.BVHModel
|
Yeah it would be nice to make them serializable! It would be nice if it was defined on the original object though (vs a separate |
But, you still need to create the FCL object in the Process right? then again, this re-occurs when you define the FCL object used the serialized dict. For example, using FCL in PyTorch's dataloader with multiprocessing support, I think it is still necessary to define a reduce method. |
Hi mikedh, I tried adding a def __iter__(self):
return iter([("a", 1), ("b", 2)])but the object is not directly picklable: In [5]: import fcl
In [6]: tf_to_pickle = fcl.Transform(R, T)
In [7]: pickled_tf = pickle.dumps(tf_to_pickle)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[7], line 1
----> 1 pickled_tf = pickle.dumps(tf_to_pickle)
File stringsource:2, in fcl.fcl.Transform.__reduce_cython__()
TypeError: no default __reduce__ due to non-trivial __cinit__Also, when I try to cache the data in the def __cinit__(self, *args):
self.args = argswhen creating the object, an In [5]: tf_to_pickle = fcl.Transform(R, T)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 tf_to_pickle = fcl.Transform(R, T)
File /mnt/e/Users/movic/python-fcl/src/fcl/fcl.pyx:56, in fcl.fcl.Transform.__cinit__()
54
55 def __cinit__(self, *args):
---> 56 self.args = args
57 if len(args) == 0:
58 self.thisptr = new defs.Transform3d()
AttributeError: 'fcl.fcl.Transform' object has no attribute 'args'so I guess the inheritance solution is still worth considering. |
Picklable objects for multithreading support.
Sometime we have a list of queries to run where each query is independent of each other. In such case, we can create a thread pool and copy the same environment in each thread to run queries in parallel. The following code gives an example of doing this:
However, objects like
fcl.BVHModel,fcl.CollisionObjectare ==unpicklable==, making it unable to serialize them, and de-serialize them in threads:My solution to address this issue is to derive subclasses from those
Cythonclass, add__init__method to cache input arguments and__reduce__method to return the cached data for pickling.Currently only support
fcl.Transform,fcl.BVHModel,fcl.CollisionObject, with this commit and a simple magic import:from fcl import fcl2 as fcl, the above example script can run in parallel.