ipython parallel push custom object

2019-04-17 05:52发布

问题:

I am unable to send object to direct view workers. Here is what I want to do:

class Test:
  def __init__(self):
    self.id = 'ddfdf'

from IPython.parallel import Client
rc = Client()
dv = rc[:]
t = Test()
dv['t'] = t
print dv['t']

NameError: name 't' is not defined

This would work if I try to push pandas object or any of the build in objects. What is the way to do it with custom object?

I tried:

dv['Test'] = Test
dv['t'] = t
print dv['t']
UnpicklingError: NEWOBJ class argument isn't a type object

回答1:

For interactively defined classes (in __main__), you do need to push the class definition, or use dill. But even this doesn't appear to work for old-style classes, which is a bug in IPython's handling of old-style classes[1]. This code works fine if you use a new-style class:

class Test(object):
    ...

instead of on old-style class. Note that old-style classes are not available in Python 3. It's generally a good idea to always use new-style classes anyway.