Skip to content

Commit fe15831

Browse files
committed
add AsyncResult.display_outputs
for republishing stdout/err/displaypub/pyout.
1 parent d72b23c commit fe15831

2 files changed

Lines changed: 139 additions & 55 deletions

File tree

‎IPython/extensions/parallelmagic.py‎

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636

3737
import ast
3838
import re
39-
import sys
4039

41-
from IPython.core.display import display
4240
from IPython.core.error import UsageError
4341
from IPython.core.magic import Magics, magics_class, line_magic
4442
from IPython.testing.skipdoctest import skip_doctest
@@ -64,7 +62,7 @@ class ParallelMagics(Magics):
6462
@skip_doctest
6563
@line_magic
6664
def result(self, parameter_s=''):
67-
"""Print the result of command i on all engines..
65+
"""Print the result of command i on all engines.
6866
6967
To use this a :class:`DirectView` instance must be created
7068
and then activated by calling its :meth:`activate` method.
@@ -97,7 +95,7 @@ def result(self, parameter_s=''):
9795
result = self.active_view.get_result(msg_ids)
9896

9997
result.get()
100-
self._display_result(result)
98+
result.display_outputs()
10199

102100
@skip_doctest
103101
@line_magic
@@ -128,7 +126,7 @@ def px(self, parameter_s=''):
128126
result = self.active_view.execute(parameter_s, silent=False, block=False)
129127
if self.active_view.block:
130128
result.get()
131-
self._display_result(result)
129+
result.display_outputs()
132130

133131
@skip_doctest
134132
@line_magic
@@ -187,54 +185,6 @@ def _disable_autopx(self):
187185
self._autopx = False
188186
print "%autopx disabled"
189187

190-
def _display_result(self, result):
191-
"""Display the output of a parallel result.
192-
"""
193-
# flush iopub, just in case
194-
rc = self.active_view.client
195-
rc._flush_iopub(rc._iopub_socket)
196-
197-
if result._single_result:
198-
# single result
199-
stdouts = [result.stdout.rstrip()]
200-
stderrs = [result.stderr.rstrip()]
201-
outputs = [result.outputs]
202-
results = [result.get()]
203-
else:
204-
stdouts = [s.rstrip() for s in result.stdout]
205-
stderrs = [s.rstrip() for s in result.stderr]
206-
outputs = result.outputs
207-
results = result.get()
208-
209-
targets = self.active_view.targets
210-
if isinstance(targets, int):
211-
targets = [targets]
212-
elif targets == 'all':
213-
targets = self.active_view.client.ids
214-
215-
# republish stdout:
216-
if any(stdouts):
217-
for eid,stdout in zip(targets, stdouts):
218-
print '[stdout:%2i]' % eid, stdout
219-
220-
# republish stderr:
221-
if any(stderrs):
222-
for eid,stderr in zip(targets, stderrs):
223-
print >> sys.stderr, '[stderr:%2i]' % eid, stderr
224-
225-
# republish displaypub output
226-
for eid,e_outputs in zip(targets, outputs):
227-
for output in e_outputs:
228-
md = output['metadata'] or {}
229-
md['engine'] = eid
230-
self.shell.display_pub.publish(output['source'], output['data'], md)
231-
232-
# finally, add pyout:
233-
for eid,r in zip(targets, results):
234-
if r.pyout:
235-
display(r)
236-
237-
238188
def pxrun_cell(self, raw_cell, store_history=False, silent=False):
239189
"""drop-in replacement for InteractiveShell.run_cell.
240190
@@ -295,7 +245,7 @@ def pxrun_cell(self, raw_cell, store_history=False, silent=False):
295245
self.shell.showtraceback()
296246
return True
297247
else:
298-
self._display_result(result)
248+
result.display_outputs()
299249
return False
300250

301251

‎IPython/parallel/client/asyncresult.py‎

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from zmq import MessageTracker
2323

24-
from IPython.core.display import clear_output
24+
from IPython.core.display import clear_output, display
2525
from IPython.external.decorator import decorator
2626
from IPython.parallel import error
2727

@@ -377,6 +377,140 @@ def wait_interactive(self, interval=1., timeout=None):
377377
sys.stdout.flush()
378378
print
379379
print "done"
380+
381+
def _republish_displaypub(self, content, eid):
382+
"""republish individual displaypub content dicts"""
383+
try:
384+
ip = get_ipython()
385+
except NameError:
386+
# displaypub is meaningless outside IPython
387+
return
388+
md = content['metadata'] or {}
389+
md['engine'] = eid
390+
ip.display_pub.publish(content['source'], content['data'], md)
391+
392+
393+
def _display_single_result(self):
394+
395+
print self.stdout
396+
print >> sys.stderr, self.stderr
397+
398+
try:
399+
get_ipython()
400+
except NameError:
401+
# displaypub is meaningless outside IPython
402+
return
403+
404+
for output in self.outputs:
405+
self._republish_displaypub(output, self.engine_id)
406+
407+
if self.pyout is not None:
408+
display(self.get())
409+
410+
@check_ready
411+
def display_outputs(self, groupby="type"):
412+
"""republish the outputs of the computation
413+
414+
Parameters
415+
----------
416+
417+
groupby : str [default: type]
418+
if 'type':
419+
Group outputs by type (show all stdout, then all stderr, etc.):
420+
421+
[stdout:1] foo
422+
[stdout:2] foo
423+
[stderr:1] bar
424+
[stderr:2] bar
425+
if 'engine':
426+
Display outputs for each engine before moving on to the next:
427+
428+
[stdout:1] foo
429+
[stderr:1] bar
430+
[stdout:2] foo
431+
[stderr:2] bar
432+
433+
if 'order':
434+
Like 'type', but further collate individual displaypub
435+
outputs. This is meant for cases of each command producing
436+
several plots, and you would like to see all of the first
437+
plots together, then all of the second plots, and so on.
438+
"""
439+
# flush iopub, just in case
440+
self._client._flush_iopub(self._client._iopub_socket)
441+
if self._single_result:
442+
self._display_single_result()
443+
return
444+
445+
stdouts = [s.rstrip() for s in self.stdout]
446+
stderrs = [s.rstrip() for s in self.stderr]
447+
pyouts = [p for p in self.pyout]
448+
output_lists = self.outputs
449+
results = self.get()
450+
451+
targets = self.engine_id
452+
453+
if groupby == "engine":
454+
for eid,stdout,stderr,outputs,r,pyout in zip(
455+
targets, stdouts, stderrs, output_lists, results, pyouts
456+
):
457+
if stdout:
458+
print '[stdout:%2i]' % eid, stdout
459+
if stderr:
460+
print '[stderr:%2i]' % eid, stderr
461+
462+
try:
463+
get_ipython()
464+
except NameError:
465+
# displaypub is meaningless outside IPython
466+
return
467+
468+
for output in outputs:
469+
self._republish_displaypub(output, eid)
470+
471+
if pyout is not None:
472+
display(r)
473+
474+
elif groupby in ('type', 'order'):
475+
# republish stdout:
476+
if any(stdouts):
477+
for eid,stdout in zip(targets, stdouts):
478+
print '[stdout:%2i]' % eid, stdout
479+
480+
# republish stderr:
481+
if any(stderrs):
482+
for eid,stderr in zip(targets, stderrs):
483+
print >> sys.stderr, '[stderr:%2i]' % eid, stderr
484+
485+
try:
486+
get_ipython()
487+
except NameError:
488+
# displaypub is meaningless outside IPython
489+
return
490+
491+
if groupby == 'order':
492+
output_dict = dict((eid, outputs) for eid,outputs in zip(targets, output_lists))
493+
N = max(len(outputs) for outputs in output_lists)
494+
for i in range(N):
495+
for eid in targets:
496+
outputs = output_dict[eid]
497+
if len(outputs) >= N:
498+
self._republish_displaypub(outputs[i], eid)
499+
else:
500+
# republish displaypub output
501+
for eid,outputs in zip(targets, output_lists):
502+
for output in outputs:
503+
self._republish_displaypub(output, eid)
504+
505+
# finally, add pyout:
506+
for eid,r,pyout in zip(targets, results, pyouts):
507+
if pyout is not None:
508+
display(r)
509+
510+
else:
511+
raise ValueError("groupby must be one of 'type', 'engine', 'collate', not %r" % groupby)
512+
513+
380514

381515

382516
class AsyncMapResult(AsyncResult):

0 commit comments

Comments
 (0)