[IPython-dev] pushing python class definition to engines

Robert Kern robert.kern@gmail....
Tue Jul 15 19:39:05 CDT 2008

Andrew Straw wrote:
> 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?)

mec.execute('class MyObj(object):\n    pass') for example.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the IPython-dev mailing list