##// END OF EJS Templates
split `%px` and `%%parallel` line/cell magics
MinRK -
Show More
@@ -38,7 +38,7 b' import ast'
38 38 import re
39 39
40 40 from IPython.core.error import UsageError
41 from IPython.core.magic import Magics, magics_class, line_magic, line_cell_magic
41 from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
42 42 from IPython.testing.skipdoctest import skip_doctest
43 43
44 44 #-----------------------------------------------------------------------------
@@ -98,8 +98,46 b' class ParallelMagics(Magics):'
98 98 result.display_outputs()
99 99
100 100 @skip_doctest
101 @line_cell_magic
102 def px(self, line='', cell=None):
101 @line_magic
102 def px(self, parameter_s=''):
103 """Executes the given python command in parallel.
104
105 To use this a :class:`DirectView` instance must be created
106 and then activated by calling its :meth:`activate` method.
107
108 Then you can do the following::
109
110 In [24]: %px a = os.getpid()
111 Parallel execution on engine(s): all
112
113 In [25]: %px print a
114 [stdout:0] 1234
115 [stdout:1] 1235
116 [stdout:2] 1236
117 [stdout:3] 1237
118 """
119 return self.parallel_execute(parameter_s)
120
121 def parallel_execute(self, cell, block=None, groupby='type'):
122 """implementation used by %px and %%parallel"""
123
124 if self.active_view is None:
125 raise UsageError(NO_ACTIVE_VIEW)
126
127 # defaults:
128 block = self.active_view.block if block is None else block
129
130 base = "Parallel" if block else "Async parallel"
131 print base + " execution on engine(s): %s" % self.active_view.targets
132
133 result = self.active_view.execute(cell, silent=False, block=False)
134 if block:
135 result.get()
136 result.display_outputs(groupby)
137
138 @skip_doctest
139 @cell_magic
140 def parallel(self, line='', cell=None):
103 141 """Executes the given python command in parallel.
104 142
105 143 Cell magic usage:
@@ -134,8 +172,8 b' class ParallelMagics(Magics):'
134 172
135 173 Then you can do the following::
136 174
137 In [24]: %px a = os.getpid()
138 Parallel execution on engine(s): all
175 In [24]: %%parallel --noblock a = os.getpid()
176 Async parallel execution on engine(s): all
139 177
140 178 In [25]: %px print a
141 179 [stdout:0] 1234
@@ -143,40 +181,25 b' class ParallelMagics(Magics):'
143 181 [stdout:2] 1236
144 182 [stdout:3] 1237
145 183 """
146
147 if self.active_view is None:
148 raise UsageError(NO_ACTIVE_VIEW)
149 184
150 # defaults:
151 block = self.active_view.block
185 block = None
152 186 groupby = 'type'
153
154 if cell is None:
155 # line magic
156 cell = line
157 else:
158 # as a cell magic, we accept args
159 opts, _ = self.parse_options(line, 'oe', 'group-outputs=', 'block', 'noblock')
160
161 if 'group-outputs' in opts:
162 groupby = opts['group-outputs']
163 elif 'o' in opts:
164 groupby = 'order'
165 elif 'e' in opts:
166 groupby = 'engine'
187 # as a cell magic, we accept args
188 opts, _ = self.parse_options(line, 'oe', 'group-outputs=', 'block', 'noblock')
189
190 if 'group-outputs' in opts:
191 groupby = opts['group-outputs']
192 elif 'o' in opts:
193 groupby = 'order'
194 elif 'e' in opts:
195 groupby = 'engine'
167 196
168 if 'block' in opts:
169 block = True
170 elif 'noblock' in opts:
171 block = False
197 if 'block' in opts:
198 block = True
199 elif 'noblock' in opts:
200 block = False
172 201
173 base = "Parallel" if self.active_view.block else "Async parallel"
174 print base + " execution on engine(s): %s" % self.active_view.targets
175
176 result = self.active_view.execute(cell, silent=False, block=False)
177 if block:
178 result.get()
179 result.display_outputs(groupby)
202 return self.parallel_execute(cell, block=block, groupby=groupby)
180 203
181 204 @skip_doctest
182 205 @line_magic
General Comments 0
You need to be logged in to leave comments. Login now