Skip to content

Commit

Permalink
fix %px magic output for single target
Browse files Browse the repository at this point in the history
parallelmagic._maybe_display expects result.stdout to be a list,
but did not handle the single-result case where it is the stdout string
itself, thus printing the first character rather than the whole string.

test included
  • Loading branch information
minrk committed Jun 10, 2011
1 parent 2676dc3 commit ee1d9ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
24 changes: 15 additions & 9 deletions IPython/extensions/parallelmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def magic_px(self, ipself, parameter_s=''):
Then you can do the following::
In [24]: %px a = 5
Parallel execution on engines: all
Parallel execution on engine(s): all
Out[24]:
<Results List>
[0] In [7]: a = 5
Expand All @@ -102,7 +102,7 @@ def magic_px(self, ipself, parameter_s=''):
if self.active_view is None:
print NO_ACTIVE_VIEW
return
print "Parallel execution on engines: %s" % self.active_view.targets
print "Parallel execution on engine(s): %s" % self.active_view.targets
result = self.active_view.execute(parameter_s, block=False)
if self.active_view.block:
result.get()
Expand All @@ -125,9 +125,9 @@ def magic_autopx(self, ipself, parameter_s=''):
%autopx to enabled
In [26]: a = 10
Parallel execution on engines: [0,1,2,3]
Parallel execution on engine(s): [0,1,2,3]
In [27]: print a
Parallel execution on engines: [0,1,2,3]
Parallel execution on engine(s): [0,1,2,3]
[stdout:0] 10
[stdout:1] 10
[stdout:2] 10
Expand Down Expand Up @@ -174,15 +174,21 @@ def _maybe_display_output(self, result):
If self.active_view.block is True, wait for the result
and display the result. Otherwise, this is a noop.
"""
if isinstance(result.stdout, basestring):
# single result
stdouts = [result.stdout.rstrip()]
else:
stdouts = [s.rstrip() for s in result.stdout]

targets = self.active_view.targets
if isinstance(targets, int):
targets = [targets]
if targets == 'all':
elif targets == 'all':
targets = self.active_view.client.ids
stdout = [s.rstrip() for s in result.stdout]
if any(stdout):
for i,eid in enumerate(targets):
print '[stdout:%i]'%eid, stdout[i]

if any(stdouts):
for eid,stdout in zip(targets, stdouts):
print '[stdout:%i]'%eid, stdout


def pxrun_cell(self, raw_cell, store_history=True):
Expand Down
1 change: 1 addition & 0 deletions IPython/parallel/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def test_magic_px_blocking(self):
sys.stdout = savestdout
sio.read()
self.assertTrue('[stdout:%i]'%v.targets in sio.buf)
self.assertTrue(sio.buf.rstrip().endswith('10'))
self.assertRaisesRemote(ZeroDivisionError, ip.magic_px, '1/0')

def test_magic_px_nonblocking(self):
Expand Down

0 comments on commit ee1d9ce

Please sign in to comment.