#1
|
|||
|
|||
Sending Objects With Pickle
Hi,
I want to send an object over the network. The object contains other variables which can't be pickled (it gives the error can't pickle webCam objects). Is there any way to pickle only part of the object (I don't care to send over the webcam object). Or another way to send my object over the network? Thanks, YG |
#2
|
|||
|
|||
Hi,
Is this object a class instance that you created? If it is then you need to override the __getstate__ and __setstate__ functions in order to bypass pickling of the webCam object. Here is an example: Code:
class MyClass: def __init__(self): self.webCam = 0 self.variable = 'something' def __getstate__(self): odict = self.__dict__.copy() # copy the dict since we change it del odict['webCam'] # remove webCam variable return odict def __setstate__(self,dict): self.webCam = 0 # initialize webCam object since it won't be in dict self.__dict__.update(dict) # update attributes |
#3
|
|||
|
|||
Hi,
I'm trying to use your method. But I think I can't get it to override def __getState__(self): I tried the following code def __getState__(self): assert 0 == 1, 'Overriden' print 5/0 odict = self.__dict__.copy() del odict['NodShakeDetector'] del odict['LookAwayDetector'] del odict['TalkingDetector'] return odict def __setstate__(self, dict): #self.webCam = 0 # initialize webCam object since it won't be in dict self.__dict__.update(dict) # update attributes But I don't think it ever calls getState, I neither get a divide by 0 error, nor an assert. And if I don't have the assert or the divide by 0 error it complains about the pickle error. |
#4
|
|||
|
|||
Heh, sorry, my bad for capitilizing getState
|
#5
|
|||
|
|||
But . . . one more question.
I have s = pickle.dumps(localStats) t = pickle.dumps(localData) network.send(s,t) And as far as I can tell nothing gets sent, I never recieve anything with that code. And I'm pretty sure the pickiling works because when I print s, t I get a long string back. I also tested network.send for integers (eg network.send(5)), and that sends fine. Is there a limit to the size of the file that network.send can take? Or can you think of another reason why the program is failing? Thanks, YG |
#6
|
|||
|
|||
Hi,
There is an internal limit in Vizard on how big a message it can receive over the network. Currently the size is 4096 bytes. The pickle string is probably exceeding this size. This is an unusually large size to be sending over the network. Are you sending these messages every frame? If possible, try to cut down the size of the message to only the relevant data. If you still need to send this much data then let me know and I will provide you with a work around. |
#7
|
|||
|
|||
Hi,
It was too large of a file size, but I found an alternate solution to the problem, thanks though. |
Thread Tools | |
Display Modes | Rate This Thread |
|
|