PDA

View Full Version : Sending Objects With Pickle


Vygreif
11-02-2005, 03:35 PM
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

farshizzo
11-02-2005, 05:08 PM
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: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

Vygreif
11-02-2005, 07:47 PM
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.

Vygreif
11-02-2005, 07:49 PM
Heh, sorry, my bad for capitilizing getState

Vygreif
11-02-2005, 08:22 PM
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

farshizzo
11-03-2005, 09:17 AM
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.

Vygreif
11-05-2005, 02:42 PM
Hi,
It was too large of a file size, but I found an alternate solution to the problem, thanks though.