ipyparallel doesn't do much in the way of serialization. It has custom zero-copy handling of numpy arrays, but other than that, it doesn't do anything other than the bare minimum to make basic interactively defined functions and classes sendable.
There are a few projects that extend pickle to make just about anything sendable, and one of these is dill. Another is cloudpickle.
To install dill:
pip install dill
First, as always, we create a task function, this time with a closure
def make_closure(a):
"""make a weird function with a closure on an open file, and return it"""
import os
f = open('/tmp/dilltest', 'a')
def has_closure(b):
product = a * b
f.write("%i: %g\n" % (os.getpid(), product))
f.flush()
return product
return has_closure
!rm -f /tmp/dilltest
closed = make_closure(5)
closed(2)
cat /tmp/dilltest
import pickle
Without help, pickle can't deal with closures
pickle.dumps(closed)
But after we import dill, magic happens
import dill
dill.dumps(closed)[:64] + b'...'
So from now on, pretty much everything is pickleable.
As usual, we start by creating our Client and View
import ipyparallel as ipp
cluster = ipp.Cluster(n=2)
cluster.start_cluster_sync()
rc = cluster.connect_client_sync()
rc.wait_for_engines(n=2)
view = rc.load_balanced_view()
Now let's try sending our function with a closure:
view.apply_sync(closed, 3)
Oops, no dice. For IPython to work with dill,
there are one or two more steps. IPython will do these for you if you call DirectView.use_dill:
rc[:].use_dill()
Now let's try again
view.apply_sync(closed, 3)
cat /tmp/dilltest
Yay! Now we can use dill to allow ipyparallel to send anything.
And that's it! We can send closures, open file handles, and other previously non-pickleables to our engines.
Let's give it a try now:
remote_closure = view.apply_sync(make_closure, 4)
remote_closure(5)
cat /tmp/dilltest
But wait, there's more!
At this point, we can send/recv all kinds of stuff
def outer(a):
def inner(b):
def inner_again(c):
return c * b * a
return inner_again
return inner
So outer returns a function with a closure, which returns a function with a closure.
Now, we can resolve the first closure on the engine, the second here, and the third on a different engine, after passing through a lambda we define here and call there, just for good measure.
view.apply_sync(lambda f: f(3),view.apply_sync(outer, 1)(2))
And for good measure, let's test that normal execution still works:
%px foo = 5
print(rc[:]['foo'])
rc[:]['bar'] = lambda : 2 * foo
rc[:].apply_sync(ipp.Reference('bar'))
And test that the @interactive decorator works
%%file testdill.py
import ipyparallel as ipp
@ipp.interactive
class C:
a = 5
@ipp.interactive
class D(C):
b = 10
@ipp.interactive
def foo(a):
return a * b
import testdill
v = rc[-1]
v['D'] = testdill.D
d = v.apply_sync(lambda : D())
print(d.a, d.b)
v['b'] = 10
v.apply_sync(testdill.foo, 5)