Skip to content

Commit

Permalink
change %result and add %%px --out foo
Browse files Browse the repository at this point in the history
%result now only displays the last result, but accepts the display-formatting options.

%%px --out=foo

stores the AsyncResult object for the execution in the user_ns.
  • Loading branch information
minrk committed Jun 12, 2012
1 parent 40ea872 commit 8df45e0
Showing 1 changed file with 69 additions and 22 deletions.
91 changes: 69 additions & 22 deletions IPython/extensions/parallelmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@


NO_ACTIVE_VIEW = "Use activate() on a DirectView object to use it with magics."
NO_LAST_RESULT = "%result recalls last %px result, which has not yet been used."


@magics_class
Expand All @@ -58,12 +59,36 @@ class ParallelMagics(Magics):
_autopx = False
# the current view used by the magics:
active_view = None

# last result cache for %result
last_result = None

@skip_doctest
@line_magic
def result(self, parameter_s=''):
"""Print the result of command i on all engines.
def result(self, line=''):
"""Print the result of the last asynchronous %px command.
Usage:
%result [-o] [-e] [--group-options=type|engine|order]
Options:
-o: collate outputs in order (same as group-outputs=order)
-e: group outputs by engine (same as group-outputs=engine)
--group-outputs=type [default behavior]:
each output type (stdout, stderr, displaypub) for all engines
displayed together.
--group-outputs=order:
The same as 'type', but individual displaypub outputs (e.g. plots)
will be interleaved, so it will display all of the first plots,
then all of the second plots, etc.
--group-outputs=engine:
All of an engine's output is displayed before moving on to the next.
To use this a :class:`DirectView` instance must be created
and then activated by calling its :meth:`activate` method.
Expand All @@ -81,21 +106,25 @@ def result(self, parameter_s=''):
[10] Out[10]: 60922
[11] Out[10]: 60923
"""
opts, _ = self.parse_options(line, 'oe', 'group-outputs=')

if 'group-outputs' in opts:
groupby = opts['group-outputs']
elif 'o' in opts:
groupby = 'order'
elif 'e' in opts:
groupby = 'engine'
else:
groupby = 'type'

if self.active_view is None:
raise UsageError(NO_ACTIVE_VIEW)

stride = len(self.active_view)
try:
index = int(parameter_s)
except:
index = -1
msg_ids = self.active_view.history[stride * index:(stride * (index + 1)) or None]

result = self.active_view.get_result(msg_ids)
if self.last_result is None:
raise UsageError(NO_LAST_RESULT)

result.get()
result.display_outputs()
self.last_result.get()
self.last_result.display_outputs(groupby=groupby)

@skip_doctest
@line_magic
Expand All @@ -118,7 +147,7 @@ def px(self, parameter_s=''):
"""
return self.parallel_execute(parameter_s)

def parallel_execute(self, cell, block=None, groupby='type'):
def parallel_execute(self, cell, block=None, groupby='type', save_name=None):
"""implementation used by %px and %%parallel"""

if self.active_view is None:
Expand All @@ -128,9 +157,20 @@ def parallel_execute(self, cell, block=None, groupby='type'):
block = self.active_view.block if block is None else block

base = "Parallel" if block else "Async parallel"
print base + " execution on engine(s): %s" % self.active_view.targets

targets = self.active_view.targets
if isinstance(targets, list) and len(targets) > 10:
str_targets = str(targets[:4])[:-1] + ', ..., ' + str(targets[-4:])[1:]
else:
str_targets = str(targets)
print base + " execution on engine(s): %s" % str_targets

result = self.active_view.execute(cell, silent=False, block=False)
self.last_result = result

if save_name:
self.shell.user_ns[save_name] = result

if block:
result.get()
result.display_outputs(groupby)
Expand All @@ -145,11 +185,14 @@ def cell_px(self, line='', cell=None):
Cell magic usage:
%%px [-o] [-e] [--group-options=type|engine|order] [--[no]block]
%%px [-o] [-e] [--group-options=type|engine|order] [--[no]block] [--out name]
Options:
Options (%%px cell magic only):
--out <name>: store the AsyncResult object for this computation
in the global namespace.
-o: collate outputs in oder (same as group-outputs=order)
-o: collate outputs in order (same as group-outputs=order)
-e: group outputs by engine (same as group-outputs=engine)
Expand All @@ -175,10 +218,12 @@ def cell_px(self, line='', cell=None):
Then you can do the following::
In [24]: %%parallel --noblock a = os.getpid()
In [24]: %%px --noblock
....: a = os.getpid()
Async parallel execution on engine(s): all
In [25]: %px print a
In [25]: %%px
....: print a
[stdout:0] 1234
[stdout:1] 1235
[stdout:2] 1236
Expand All @@ -188,7 +233,7 @@ def cell_px(self, line='', cell=None):
block = None
groupby = 'type'
# as a cell magic, we accept args
opts, _ = self.parse_options(line, 'oe', 'group-outputs=', 'block', 'noblock')
opts, _ = self.parse_options(line, 'oe', 'group-outputs=', 'out=', 'block', 'noblock')

if 'group-outputs' in opts:
groupby = opts['group-outputs']
Expand All @@ -202,7 +247,9 @@ def cell_px(self, line='', cell=None):
elif 'noblock' in opts:
block = False

return self.parallel_execute(cell, block=block, groupby=groupby)
save_name = opts.get('out')

return self.parallel_execute(cell, block=block, groupby=groupby, save_name=save_name)

@skip_doctest
@line_magic
Expand Down

0 comments on commit 8df45e0

Please sign in to comment.