<div dir="ltr">This worked well for the example that I posted. Thanks again.<div><br></div><div>However, it brings up another question. What are the restrictions on using this on methods inside a custom object? For example, if I change "functs.py" into an object with the following methods:</div><div><br></div><div><div>    @interactive</div><div>    def _do_this(self, fname):</div><div>        f = File(fname)</div><div>        f.extra = 'hello'</div><div>        self._do_something_else(f)</div><div>        return f</div><div><br></div><div>    @interactive</div><div>    def _do_something_else(self, f):</div><div>        f.junk = 'World'</div></div><div><br></div><div>I don't get any errors, but "_do_something_else" doesn't seem to be called, but "_do_this" works fine. (Actually, in my larger code, I'm getting a pickling error... But that's tough to reproduce.)</div><div><br></div><div>Ryan</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 17, 2015 at 4:17 PM, Ryan Nelson <span dir="ltr"><<a href="mailto:rnelsonchem@gmail.com" target="_blank">rnelsonchem@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Thanks!<div><br></div><div>Searching for that decorator brings up a bunch of search hits. However, it looks like it isn't really documented in the main document yet (?).</div><div><br></div><div>I'll try this out.</div><span class=""><font color="#888888"><div><br></div><div>Ryan </div><div><br></div><div><br></div></font></span></div><div class=""><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 17, 2015 at 3:15 PM, MinRK <span dir="ltr"><<a href="mailto:benjaminrk@gmail.com" target="_blank">benjaminrk@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><p style="margin:1.2em 0px!important">This is one of the most common sources of confusion with IPython.parallel. When you call a function, it’s globals are resolved to its own module. So when you call <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">_do_this</code>, it is looking for <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">File</code> in <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">mymod</code>, not in the interactive namespace. If you had defined <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">_do_this</code> interactively (in <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">__main__</code>), it would have found <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">File</code>. Similarly, if <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">File</code> were defined in the same module as <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">_do_this</code>, it would also be found.</p>
<p style="margin:1.2em 0px!important">IPython provides a decorator that lets you define functions in modules that will be treated as if they were defined in <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">__main__</code>, and will thus have access to the interactive namespace.</p>
<pre style="font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em;color:rgb(51,51,51);display:block!important;background:rgb(248,248,248)"><span style="color:rgb(51,51,51);font-weight:bold">from</span> IPython.parallel <span style="color:rgb(51,51,51);font-weight:bold">import</span> interactive

<span>@interactive</span>
<span><span style="color:rgb(51,51,51);font-weight:bold">def</span> <span style="color:rgb(153,0,0);font-weight:bold">_do_this</span><span>(fname)</span>:</span>
    f = File(fname)
    ...
</code></pre>
<p style="margin:1.2em 0px!important">-MinRK</p>
<div title="MDH:VGhpcyBpcyBvbmUgb2YgdGhlIG1vc3QgY29tbW9uIHNvdXJjZXMgb2YgY29uZnVzaW9uIHdpdGgg
SVB5dGhvbi5wYXJhbGxlbC4gV2hlbiB5b3UgY2FsbCBhIGZ1bmN0aW9uLCBpdCdzIGdsb2JhbHMg
YXJlIHJlc29sdmVkIHRvIGl0cyBvd24gbW9kdWxlLiBTbyB3aGVuIHlvdSBjYWxsIGBfZG9fdGhp
c2AsIGl0IGlzIGxvb2tpbmcgZm9yIGBGaWxlYCBpbiBgbXltb2RgLCBub3QgaW4gdGhlIGludGVy
YWN0aXZlIG5hbWVzcGFjZS4gSWYgeW91IGhhZCBkZWZpbmVkIGBfZG9fdGhpc2AgaW50ZXJhY3Rp
dmVseSAoaW4gYF9fbWFpbl9fYCksIGl0IHdvdWxkIGhhdmUgZm91bmQgYEZpbGVgLiBTaW1pbGFy
bHksIGlmIGBGaWxlYCB3ZXJlIGRlZmluZWQgaW4gdGhlIHNhbWUgbW9kdWxlIGFzIGBfZG9fdGhp
c2AsIGl0IHdvdWxkIGFsc28gYmUgZm91bmQuPGRpdj48YnI+PC9kaXY+PGRpdj5JUHl0aG9uIHBy
b3ZpZGVzIGEgZGVjb3JhdG9yIHRoYXQgbGV0cyB5b3UgZGVmaW5lIGZ1bmN0aW9ucyBpbiBtb2R1
bGVzIHRoYXQgd2lsbCBiZSB0cmVhdGVkIGFzIGlmIHRoZXkgd2VyZSBkZWZpbmVkIGluIGBfX21h
aW5fX2AsIGFuZCB3aWxsIHRodXMgaGF2ZSBhY2Nlc3MgdG8gdGhlIGludGVyYWN0aXZlIG5hbWVz
cGFjZS48L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2PmBgYHB5dGhvbjwvZGl2PjxkaXY+ZnJvbSBJ
UHl0aG9uLnBhcmFsbGVsIGltcG9ydCBpbnRlcmFjdGl2ZTwvZGl2PjxkaXY+PGJyPjwvZGl2Pjxk
aXY+QGludGVyYWN0aXZlPC9kaXY+PGRpdj5kZWYgX2RvX3RoaXMoZm5hbWUpOjwvZGl2PjxkaXY+
Jm5ic3A7ICZuYnNwOyBmID0gRmlsZShmbmFtZSk8L2Rpdj48ZGl2PiZuYnNwOyAmbmJzcDsgLi4u
PC9kaXY+PGRpdj5gYGA8L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2Pi1NaW5SSzwvZGl2PjxkaXY+
PGJyPjwvZGl2Pg==" style="min-height:0px;width:0px;max-height:0px;max-width:0px;overflow:hidden;font-size:0em;padding:0px;margin:0px">​</div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Sat, Jan 17, 2015 at 11:14 AM, Ryan Nelson <span dir="ltr"><<a href="mailto:rnelsonchem@gmail.com" target="_blank">rnelsonchem@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div><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>
<br></div></div>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org" target="_blank">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org" target="_blank">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div></div>