Show More
@@ -11,15 +11,15 b' Usage' | |||||
11 |
|
11 | |||
12 | ``%autopx`` |
|
12 | ``%autopx`` | |
13 |
|
13 | |||
14 |
|
|
14 | {AUTOPX_DOC} | |
15 |
|
15 | |||
16 | ``%px`` |
|
16 | ``%px`` | |
17 |
|
17 | |||
18 |
|
|
18 | {PX_DOC} | |
19 |
|
19 | |||
20 | ``%result`` |
|
20 | ``%result`` | |
21 |
|
21 | |||
22 |
|
|
22 | {RESULT_DOC} | |
23 |
|
23 | |||
24 | """ |
|
24 | """ | |
25 |
|
25 | |||
@@ -37,6 +37,7 b' Usage' | |||||
37 | import ast |
|
37 | import ast | |
38 | import re |
|
38 | import re | |
39 |
|
39 | |||
|
40 | from IPython.core.error import UsageError | |||
40 | from IPython.core.magic import Magics, magics_class, line_magic |
|
41 | from IPython.core.magic import Magics, magics_class, line_magic | |
41 | from IPython.testing.skipdoctest import skip_doctest |
|
42 | from IPython.testing.skipdoctest import skip_doctest | |
42 |
|
43 | |||
@@ -44,9 +45,8 b' from IPython.testing.skipdoctest import skip_doctest' | |||||
44 | # Definitions of magic functions for use with IPython |
|
45 | # Definitions of magic functions for use with IPython | |
45 | #----------------------------------------------------------------------------- |
|
46 | #----------------------------------------------------------------------------- | |
46 |
|
47 | |||
47 | NO_ACTIVE_VIEW = """ |
|
48 | ||
48 |
Use activate() on a DirectView object to |
|
49 | NO_ACTIVE_VIEW = "Use activate() on a DirectView object to use it with magics." | |
49 | """ |
|
|||
50 |
|
50 | |||
51 |
|
51 | |||
52 | @magics_class |
|
52 | @magics_class | |
@@ -54,10 +54,10 b' class ParallelMagics(Magics):' | |||||
54 | """A set of magics useful when controlling a parallel IPython cluster. |
|
54 | """A set of magics useful when controlling a parallel IPython cluster. | |
55 | """ |
|
55 | """ | |
56 |
|
56 | |||
57 | def __init__(self, shell): |
|
57 | # A flag showing if autopx is activated or not | |
58 | super(ParallelMagics, self).__init__(shell) |
|
58 | _autopx = False | |
59 | # A flag showing if autopx is activated or not |
|
59 | # the current view used by the magics: | |
60 | self.autopx = False |
|
60 | active_view = None | |
61 |
|
61 | |||
62 | @skip_doctest |
|
62 | @skip_doctest | |
63 | @line_magic |
|
63 | @line_magic | |
@@ -70,20 +70,12 b' class ParallelMagics(Magics):' | |||||
70 | Then you can do the following:: |
|
70 | Then you can do the following:: | |
71 |
|
71 | |||
72 | In [23]: %result |
|
72 | In [23]: %result | |
73 | Out[23]: |
|
73 | Out[23]: <AsyncResult: unknown> | |
74 | <Results List> |
|
74 | ||
75 |
|
|
75 | In [22]: ar = %result 6 | |
76 | [1] In [6]: a = 10 |
|
|||
77 |
|
||||
78 | In [22]: %result 6 |
|
|||
79 | Out[22]: |
|
|||
80 | <Results List> |
|
|||
81 | [0] In [6]: a = 10 |
|
|||
82 | [1] In [6]: a = 10 |
|
|||
83 | """ |
|
76 | """ | |
84 | if self.active_view is None: |
|
77 | if self.active_view is None: | |
85 |
|
|
78 | raise UsageError(NO_ACTIVE_VIEW) | |
86 | return |
|
|||
87 |
|
79 | |||
88 | try: |
|
80 | try: | |
89 | index = int(parameter_s) |
|
81 | index = int(parameter_s) | |
@@ -102,17 +94,18 b' class ParallelMagics(Magics):' | |||||
102 |
|
94 | |||
103 | Then you can do the following:: |
|
95 | Then you can do the following:: | |
104 |
|
96 | |||
105 |
In [24]: %px a = |
|
97 | In [24]: %px a = os.getpid() | |
106 | Parallel execution on engine(s): all |
|
98 | Parallel execution on engine(s): all | |
107 |
|
|
99 | ||
108 | <Results List> |
|
100 | In [25]: %px print a | |
109 | [0] In [7]: a = 5 |
|
101 | [stdout:0] 1234 | |
110 |
[ |
|
102 | [stdout:1] 1235 | |
|
103 | [stdout:2] 1236 | |||
|
104 | [stdout:3] 1237 | |||
111 | """ |
|
105 | """ | |
112 |
|
106 | |||
113 | if self.active_view is None: |
|
107 | if self.active_view is None: | |
114 |
|
|
108 | raise UsageError(NO_ACTIVE_VIEW) | |
115 | return |
|
|||
116 | print "Parallel execution on engine(s): %s" % self.active_view.targets |
|
109 | print "Parallel execution on engine(s): %s" % self.active_view.targets | |
117 | result = self.active_view.execute(parameter_s, block=False) |
|
110 | result = self.active_view.execute(parameter_s, block=False) | |
118 | if self.active_view.block: |
|
111 | if self.active_view.block: | |
@@ -149,7 +142,7 b' class ParallelMagics(Magics):' | |||||
149 | In [27]: %autopx |
|
142 | In [27]: %autopx | |
150 | %autopx disabled |
|
143 | %autopx disabled | |
151 | """ |
|
144 | """ | |
152 | if self.autopx: |
|
145 | if self._autopx: | |
153 | self._disable_autopx() |
|
146 | self._disable_autopx() | |
154 | else: |
|
147 | else: | |
155 | self._enable_autopx() |
|
148 | self._enable_autopx() | |
@@ -159,38 +152,38 b' class ParallelMagics(Magics):' | |||||
159 | pxrun_cell. |
|
152 | pxrun_cell. | |
160 | """ |
|
153 | """ | |
161 | if self.active_view is None: |
|
154 | if self.active_view is None: | |
162 |
|
|
155 | raise UsageError(NO_ACTIVE_VIEW) | |
163 | return |
|
|||
164 |
|
156 | |||
165 |
# override run_cell |
|
157 | # override run_cell | |
166 | self._original_run_cell = self.shell.run_cell |
|
158 | self._original_run_cell = self.shell.run_cell | |
167 | self.shell.run_cell = self.pxrun_cell |
|
159 | self.shell.run_cell = self.pxrun_cell | |
168 | self._original_run_code = self.shell.run_code |
|
|||
169 | self.shell.run_code = self.pxrun_code |
|
|||
170 |
|
160 | |||
171 | self.autopx = True |
|
161 | self._autopx = True | |
172 | print "%autopx enabled" |
|
162 | print "%autopx enabled" | |
173 |
|
163 | |||
174 | def _disable_autopx(self): |
|
164 | def _disable_autopx(self): | |
175 | """Disable %autopx by restoring the original InteractiveShell.run_cell. |
|
165 | """Disable %autopx by restoring the original InteractiveShell.run_cell. | |
176 | """ |
|
166 | """ | |
177 | if self.autopx: |
|
167 | if self._autopx: | |
178 | self.shell.run_cell = self._original_run_cell |
|
168 | self.shell.run_cell = self._original_run_cell | |
179 | self.shell.run_code = self._original_run_code |
|
169 | self._autopx = False | |
180 | self.autopx = False |
|
|||
181 | print "%autopx disabled" |
|
170 | print "%autopx disabled" | |
182 |
|
171 | |||
183 |
def |
|
172 | def _display_result(self, result): | |
184 |
""" |
|
173 | """Display the output of a parallel result. | |
185 |
|
174 | |||
186 | If self.active_view.block is True, wait for the result |
|
175 | If self.active_view.block is True, wait for the result | |
187 | and display the result. Otherwise, this is a noop. |
|
176 | and display the result. Otherwise, this is a noop. | |
188 | """ |
|
177 | """ | |
189 | if isinstance(result.stdout, basestring): |
|
178 | if result._single_result: | |
190 | # single result |
|
179 | # single result | |
191 | stdouts = [result.stdout.rstrip()] |
|
180 | stdouts = [result.stdout.rstrip()] | |
|
181 | stderrs = [result.stderr.rstrip()] | |||
192 | else: |
|
182 | else: | |
193 | stdouts = [s.rstrip() for s in result.stdout] |
|
183 | stdouts = [s.rstrip() for s in result.stdout] | |
|
184 | stderrs = [s.rstrip() for s in result.stderr] | |||
|
185 | ||||
|
186 | results = result.get_dict() | |||
194 |
|
187 | |||
195 | targets = self.active_view.targets |
|
188 | targets = self.active_view.targets | |
196 | if isinstance(targets, int): |
|
189 | if isinstance(targets, int): | |
@@ -266,44 +259,12 b' class ParallelMagics(Magics):' | |||||
266 | self._maybe_display_output(result) |
|
259 | self._maybe_display_output(result) | |
267 | return False |
|
260 | return False | |
268 |
|
261 | |||
269 | def pxrun_code(self, code_obj): |
|
|||
270 | """drop-in replacement for InteractiveShell.run_code. |
|
|||
271 |
|
||||
272 | This executes code remotely, instead of in the local namespace. |
|
|||
273 |
|
||||
274 | See InteractiveShell.run_code for details. |
|
|||
275 | """ |
|
|||
276 | ipself = self.shell |
|
|||
277 | # check code object for the autopx magic |
|
|||
278 | if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names \ |
|
|||
279 | and any( [ isinstance(c, basestring) and 'autopx' in c |
|
|||
280 | for c in code_obj.co_consts ]): |
|
|||
281 | self._disable_autopx() |
|
|||
282 | return False |
|
|||
283 | else: |
|
|||
284 | try: |
|
|||
285 | result = self.active_view.execute(code_obj, block=False) |
|
|||
286 | except: |
|
|||
287 | ipself.showtraceback() |
|
|||
288 | return True |
|
|||
289 | else: |
|
|||
290 | if self.active_view.block: |
|
|||
291 | try: |
|
|||
292 | result.get() |
|
|||
293 | except: |
|
|||
294 | self.shell.showtraceback() |
|
|||
295 | return True |
|
|||
296 | else: |
|
|||
297 | self._maybe_display_output(result) |
|
|||
298 | return False |
|
|||
299 |
|
||||
300 |
|
262 | |||
301 |
__doc__ = __doc__. |
|
263 | __doc__ = __doc__.format( | |
302 |
|
|
264 | AUTOPX_DOC = ' '*8 + ParallelMagics.autopx.__doc__, | |
303 | __doc__ = __doc__.replace('@PX_DOC@', |
|
265 | PX_DOC = ' '*8 + ParallelMagics.px.__doc__, | |
304 |
|
|
266 | RESULT_DOC = ' '*8 + ParallelMagics.result.__doc__ | |
305 | __doc__ = __doc__.replace('@RESULT_DOC@', |
|
267 | ) | |
306 | " " + ParallelMagics.result.__doc__) |
|
|||
307 |
|
268 | |||
308 | _loaded = False |
|
269 | _loaded = False | |
309 |
|
270 |
General Comments 0
You need to be logged in to leave comments.
Login now