<div dir="ltr">Hello everyone,<div><br></div><div>I'm developing a module for processing some data files. There are several steps to this process, which can be time consuming, so I would like to write a helper function in my module that uses IPython.parallel to spread out the work over several processors. Unfortunately, I'm having trouble getting things to work properly. Note: I can get something similar to work when it is not written as a module...</div><div><br></div><div>I tried my best to make a simple example that fails with the same error that I'm seeing in the larger case (below); however, it is still a little complicated. </div><div><br></div><div>After running the test script, I get a "NameError" on the cluster nodes (traceback below). If I check the nodes manually, the objects are defined. I can also define new functions from the IPython terminal that act on those objects, and those work as expected.</div><div><br></div><div>Any help you can provide is most appreciated. Sorry if this is a duplicate, but I couldn't find it anywhere else.</div><div><br></div><div>Python 3.4 and 2.7 and IPython 2.3.1</div><div><br></div><div>Ryan</div><div><br></div><div>________________________________________</div><div><br></div><div>Here's the approximate structure of my module:</div><div><br></div><div>mymod/</div><div>    __init__.py</div><div>    functs.py : helper functions with Parallel support</div><div>    objects.py : contains my custom processing objects</div><div><br></div><div># functs.py #<br></div><div>-------------</div><div><div>from IPython.parallel import Client</div><div><br></div><div>import mymod.objects as objects</div><div><br></div><div>def process(files):</div><div>    client = Client()</div><div>    dview = client[:]</div><div>    dview.block = True</div><div><br></div><div>    # Do this to make module path known</div><div>    # However, my mod will eventually be installed via pip, so this </div><div>    # isn't necessary to reproduce error in that case</div><div>    with dview.sync_imports():</div><div>        import sys</div><div>    sys.path.append(['/home/nelson/code/testing/',])</div><div>    </div><div>    dview['sys.path'] = sys.path</div><div>    File = objects.File1</div><div>    dview['File'] = File</div><div>    result = dview.map_sync(_do_this, files)</div><div>    return result</div><div><br></div><div>def _do_this(fname):</div><div>    f = File(fname)</div><div>    f.extra = 'hello'</div><div>    return f</div></div><div>-------------<br></div><div><br></div><div># objects.py #</div><div>-------------<br></div><div><div>class File1(object):</div><div>    def __init__(self, fname):</div><div>        self.fname = fname</div></div><div>-------------<br></div><div><br></div><div>I also have a test script ("test.py") with the following:</div><div>-------------<br></div><div>from mymod.functs import process</div><div>result = process(['a', 'b', 'c', 'd', 'e', 'f', 'g'])</div><div>[print(i.fname, i.extra) for i in result]</div><div>-------------<br></div><div><br></div><div>When I run this script, I get the following errors:</div><div><br></div><div><div><div>importing sys on engine(s)</div><div>Traceback (most recent call last):</div><div>  File "test2.py", line 3, in <module></div><div>    result = process(['a', 'b', 'c', 'd', 'e', 'f', 'g'])</div><div>  File "/home/nelson/code/testing/mymod/functs.py", line 16, in process</div><div>    result = dview.map_sync(_do_this, files)</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/view.py", line 366, in map_sync</div><div>    return self.map(f,*sequences,**kwargs)</div><div>  File "<string>", line 2, in map</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/view.py", line 66, in sync_results</div><div>    ret = f(self, *args, **kwargs)</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/view.py", line 624, in map</div><div>    return pf.map(*sequences)</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/remotefunction.py", line 271, in map</div><div>    ret = self(*sequences)</div><div>  File "<string>", line 2, in __call__</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/remotefunction.py", line 78, in sync_view_results</div><div>    return f(self, *args, **kwargs)</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/remotefunction.py", line 254, in __call__</div><div>    return r.get()</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/asyncresult.py", line 118, in get</div><div>    raise self._exception</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/client/asyncresult.py", line 153, in wait</div><div>    results = error.collect_exceptions(results, self._fname)</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/error.py", line 233, in collect_exceptions</div><div>    raise e</div><div>  File "/usr/lib64/python3.4/site-packages/IPython/parallel/error.py", line 231, in collect_exceptions</div><div>    raise CompositeError(msg, elist)</div><div>IPython.parallel.error.CompositeError: one or more exceptions from call to method: _do_this</div><div>[0:apply]: NameError: name 'File' is not defined</div><div>[1:apply]: NameError: name 'File' is not defined</div><div>[2:apply]: NameError: name 'File' is not defined</div><div>[3:apply]: NameError: name 'File' is not defined</div></div></div><div><br></div><div>However, if I jump into an interactive Python terminal, these objects are defined on the cluster nodes.</div><div><div>In [1]: from IPython.parallel import Client</div><div><br></div><div>In [2]: client = Client()</div><div><br></div><div>In [3]: dview = client[:]</div><div><br></div><div>In [4]: dview['File']                                                                                                                                                                                              </div><div>Out[4]: </div><div>[mymod.objects.File1,</div><div> mymod.objects.File1,</div><div> mymod.objects.File1,</div><div> mymod.objects.File1]</div></div><div><br></div><div><br></div></div>