[IPython-dev] pushing python class definition to engines

Andrew Straw strawman@astraw....
Tue Jul 15 19:30:35 CDT 2008


Robert Kern wrote:
> Andrew Straw wrote:
>   
>> After seeing Brian Granger's great demo of the parallel abilities in
>> IPython last week at the SIAM conference, I'm playing with ipython trunk
>> which I just pulled out of launchpad (for this reason, I'm posting on
>> the -dev list).
>>
>> Anyhow, if I start a client in the usual way:
>>
>> from IPython.kernel import client
>> mec = client.MultiEngineClient()
>>
>> I can easily push Python integers:
>>
>> a=5
>> mec['a'] = a
>> print mec.gather('a')
>>
>> But pushing classes doesn't work:
>>
>> class MyObj(object):
>>     def __init__(self,x):
>>         print 'PID',os.getpid()
>>         self.x=x
>>
>> mec['MyObj'] = MyObj
>> print mec.gather('MyObj')
>>
>> That gives the following error. How can I push a class definition to my
>> engines?
>>     
>
> It's really tricky since class objects normally don't really serialize. Pickles 
> just contain a reference to the fully-qualified name of the class. In this case, 
> it's __main__.MyObj. Since there is no MyObj in __main__ in your remote 
> namespace, you get the error you see. You will have to push actual code to your 
> remote kernels.
>   
OK. Thanks for the info. How do I do that/what do you mean? (Do I simply
make a string with my class definition and push that across?)

-Andrew


More information about the IPython-dev mailing list