diff --git a/py3/__init__.py b/py3/__init__.py index 4e1c01f..e3cb261 100644 --- a/py3/__init__.py +++ b/py3/__init__.py @@ -2660,7 +2660,7 @@ def __init__(self, computation, nodes=None, depends=[], job_status=None, cluster if inspect.isfunction(setup): if setup.__defaults__: print('\n dispy does not support calling "setup" with keyword arguments\n') - depends.append(setup) + depends.insert(0, setup) compute.setup = setup.__name__ compute.setup_args_count = setup.__code__.co_argcount elif isinstance(setup, functools.partial): @@ -2761,8 +2761,9 @@ def __init__(self, computation, nodes=None, depends=[], job_status=None, cluster elif (inspect.isfunction(dep) or inspect.isclass(dep) or (hasattr(dep, '__class__') and hasattr(dep, '__module__'))): + immediate=None if inspect.isfunction(dep) or inspect.isclass(dep): - pass + immediate = dep.__name__ if dep.__name__.startswith('nodeinit_') else None elif hasattr(dep, '__class__') and inspect.isclass(dep.__class__): dep = dep.__class__ try: @@ -2772,6 +2773,8 @@ def __init__(self, computation, nodes=None, depends=[], job_status=None, cluster raise lines[0] = lines[0].lstrip() compute.code += '\n' + ''.join(lines) + if immediate: + compute.code += '\n' + immediate + '()\n' elif isinstance(dep, functools.partial): try: lines = inspect.getsourcelines(dep.func)[0] diff --git a/py3/examples/obj_instances.py b/py3/examples/obj_instances.py index 0ac7c21..9696ca9 100644 --- a/py3/examples/obj_instances.py +++ b/py3/examples/obj_instances.py @@ -1,6 +1,9 @@ # example program that sends object instances in local program # as arguments to distributed computation -class C: +# Also shows use of nodeinit_ feature to ensure that cmd module is immediately imported so that class C can extend cmd.Cmd on the node +import cmd + +class C(cmd.Cmd): def __init__(self, i, n): self.i = i self.n = n @@ -17,7 +20,11 @@ def compute(obj): if __name__ == '__main__': import random, dispy - cluster = dispy.JobCluster(compute, depends=[C]) + def nodeinit_setup(): + global cmd + import cmd + return 0 + cluster = dispy.JobCluster(compute, depends=[C], setup=nodeinit_setup) jobs = [] for i in range(10): c = C(i, random.uniform(1, 3)) # create object of C