因為只有direct view支援use_dill() function, 如果loadbalace view也要使用dill時,可使用以下方法 (stackoverflow)。
如果要使用自定義的class時,還要考慮Data movement via DirectView, 否則無法呼叫class instance method,rc = Client()
rc.direct_view().use_dill()
lv = rc.load_balanced_view()
class MyObject(object):
def __init__(self, val):
import os
import platform
self.name = platform.node()
self.pid = os.getpid()
self.val = val
def run(self):
return "{}, myobject {}_{}".format(self.val, self.name, self.pid)
def MyObjectrun(val):
obj = MyObject(val)
return obj.run()
def test_parallel():
rc = Client(profile='ssh')
rc.block = True
print "nodes: {}".format(len(rc.ids))
rc.direct_view().use_dill()
rc.direct_view()['MyObject'] = MyObject
lview = rc.load_balanced_view()
res = lview.apply_sync(MyObjectrun, 10)
print res