fvutils/pyvsc

PicklingError when trying to pickle a VSC randobj

smccans opened this issue · 2 comments

Is it possible to pickle a class that has the @vsc.randobj decorator? I get a PicklingError when trying to do something like this code below. Using the dill module instead of pickle results in the same error.

import vsc
import pickle

@vsc.randobj
class Sample():
    pass

obj = Sample()
string = pickle.dumps(obj)

Running that results in this error:

Traceback (most recent call last):
  File "test_vsc.py", line 10, in <module>
    string = pickle.dumps(obj)
_pickle.PicklingError: Can't pickle <class 'vsc.rand_obj.Sample'>: attribute lookup Sample on vsc.rand_obj failed

Good question, @smccans. Can you share a bit more about your intended usecase? My understanding is that Pickling is used to capture some 'persistent' view of an object that can be stored and/or transported, then 'restored' to its original state. I'll have to give thought to what state, exactly, would need to be stored in order to reconstruct the object.
I will try to read up a bit on Pickling. I suspect there are some hooks that can be used to properly handle corner cases like 'vsc.rand_obj'.

-Matthew

I'm trying to create a vsc.rand_obj and do some randomization on one system and then send that object over the network to one or more other systems. I could just send the seed and do the randomization again on the other systems, but I thought I might get a little better performance by just doing the randomization once.
As a workaround for now, I'm using jsonpickle to encode the vsc.rand_obj as a json string. Then I replace vsc.rand_obj in that json string with the correct module path (__main__ in my original example). This seems to be working so far.