Show More
@@ -1,345 +1,377 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Test Parallel magics |
|
3 | 3 | |
|
4 | 4 | Authors: |
|
5 | 5 | |
|
6 | 6 | * Min RK |
|
7 | 7 | """ |
|
8 | 8 | #------------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #------------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #------------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #------------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | import re |
|
20 | 20 | import sys |
|
21 | 21 | import time |
|
22 | 22 | |
|
23 | 23 | import zmq |
|
24 | 24 | from nose import SkipTest |
|
25 | 25 | |
|
26 | 26 | from IPython.testing import decorators as dec |
|
27 | 27 | from IPython.testing.ipunittest import ParametricTestCase |
|
28 | 28 | from IPython.utils.io import capture_output |
|
29 | 29 | |
|
30 | 30 | from IPython import parallel as pmod |
|
31 | 31 | from IPython.parallel import error |
|
32 | 32 | from IPython.parallel import AsyncResult |
|
33 | 33 | from IPython.parallel.util import interactive |
|
34 | 34 | |
|
35 | 35 | from IPython.parallel.tests import add_engines |
|
36 | 36 | |
|
37 | 37 | from .clienttest import ClusterTestCase, generate_output |
|
38 | 38 | |
|
39 | 39 | def setup(): |
|
40 | 40 | add_engines(3, total=True) |
|
41 | 41 | |
|
42 | 42 | class TestParallelMagics(ClusterTestCase, ParametricTestCase): |
|
43 | 43 | |
|
44 | 44 | def test_px_blocking(self): |
|
45 | 45 | ip = get_ipython() |
|
46 | 46 | v = self.client[-1:] |
|
47 | 47 | v.activate() |
|
48 | 48 | v.block=True |
|
49 | 49 | |
|
50 | 50 | ip.magic('px a=5') |
|
51 | 51 | self.assertEquals(v['a'], [5]) |
|
52 | 52 | ip.magic('px a=10') |
|
53 | 53 | self.assertEquals(v['a'], [10]) |
|
54 | 54 | # just 'print a' works ~99% of the time, but this ensures that |
|
55 | 55 | # the stdout message has arrived when the result is finished: |
|
56 | 56 | with capture_output() as io: |
|
57 | 57 | ip.magic( |
|
58 | 58 | 'px import sys,time;print(a);sys.stdout.flush();time.sleep(0.2)' |
|
59 | 59 | ) |
|
60 | 60 | out = io.stdout |
|
61 | 61 | self.assertTrue('[stdout:' in out, out) |
|
62 | 62 | self.assertFalse('\n\n' in out) |
|
63 | 63 | self.assertTrue(out.rstrip().endswith('10')) |
|
64 | 64 | self.assertRaisesRemote(ZeroDivisionError, ip.magic, 'px 1/0') |
|
65 | 65 | |
|
66 | 66 | def _check_generated_stderr(self, stderr, n): |
|
67 | 67 | expected = [ |
|
68 | 68 | r'\[stderr:\d+\]', |
|
69 | 69 | '^stderr$', |
|
70 | 70 | '^stderr2$', |
|
71 | 71 | ] * n |
|
72 | 72 | |
|
73 | 73 | self.assertFalse('\n\n' in stderr, stderr) |
|
74 | 74 | lines = stderr.splitlines() |
|
75 | 75 | self.assertEquals(len(lines), len(expected), stderr) |
|
76 | 76 | for line,expect in zip(lines, expected): |
|
77 | 77 | if isinstance(expect, str): |
|
78 | 78 | expect = [expect] |
|
79 | 79 | for ex in expect: |
|
80 | 80 | self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line)) |
|
81 | 81 | |
|
82 | 82 | def test_cellpx_block_args(self): |
|
83 | 83 | """%%px --[no]block flags work""" |
|
84 | 84 | ip = get_ipython() |
|
85 | 85 | v = self.client[-1:] |
|
86 | 86 | v.activate() |
|
87 | 87 | v.block=False |
|
88 | 88 | |
|
89 | 89 | for block in (True, False): |
|
90 | 90 | v.block = block |
|
91 | 91 | |
|
92 | 92 | with capture_output() as io: |
|
93 | 93 | ip.run_cell_magic("px", "", "1") |
|
94 | 94 | if block: |
|
95 | 95 | self.assertTrue(io.stdout.startswith("Parallel"), io.stdout) |
|
96 | 96 | else: |
|
97 | 97 | self.assertTrue(io.stdout.startswith("Async"), io.stdout) |
|
98 | 98 | |
|
99 | 99 | with capture_output() as io: |
|
100 | 100 | ip.run_cell_magic("px", "--block", "1") |
|
101 | 101 | self.assertTrue(io.stdout.startswith("Parallel"), io.stdout) |
|
102 | 102 | |
|
103 | 103 | with capture_output() as io: |
|
104 | 104 | ip.run_cell_magic("px", "--noblock", "1") |
|
105 | 105 | self.assertTrue(io.stdout.startswith("Async"), io.stdout) |
|
106 | 106 | |
|
107 | 107 | def test_cellpx_groupby_engine(self): |
|
108 | 108 | """%%px --group-outputs=engine""" |
|
109 | 109 | ip = get_ipython() |
|
110 | 110 | v = self.client[:] |
|
111 | 111 | v.block = True |
|
112 | 112 | v.activate() |
|
113 | 113 | |
|
114 | 114 | v['generate_output'] = generate_output |
|
115 | 115 | |
|
116 | 116 | with capture_output() as io: |
|
117 | 117 | ip.run_cell_magic('px', '--group-outputs=engine', 'generate_output()') |
|
118 | 118 | |
|
119 | 119 | self.assertFalse('\n\n' in io.stdout) |
|
120 | 120 | lines = io.stdout.splitlines()[1:] |
|
121 | 121 | expected = [ |
|
122 | 122 | r'\[stdout:\d+\]', |
|
123 | 123 | 'stdout', |
|
124 | 124 | 'stdout2', |
|
125 | 125 | r'\[output:\d+\]', |
|
126 | 126 | r'IPython\.core\.display\.HTML', |
|
127 | 127 | r'IPython\.core\.display\.Math', |
|
128 | 128 | r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math', |
|
129 | 129 | ] * len(v) |
|
130 | 130 | |
|
131 | 131 | self.assertEquals(len(lines), len(expected), io.stdout) |
|
132 | 132 | for line,expect in zip(lines, expected): |
|
133 | 133 | if isinstance(expect, str): |
|
134 | 134 | expect = [expect] |
|
135 | 135 | for ex in expect: |
|
136 | 136 | self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line)) |
|
137 | 137 | |
|
138 | 138 | self._check_generated_stderr(io.stderr, len(v)) |
|
139 | 139 | |
|
140 | 140 | |
|
141 | 141 | def test_cellpx_groupby_order(self): |
|
142 | 142 | """%%px --group-outputs=order""" |
|
143 | 143 | ip = get_ipython() |
|
144 | 144 | v = self.client[:] |
|
145 | 145 | v.block = True |
|
146 | 146 | v.activate() |
|
147 | 147 | |
|
148 | 148 | v['generate_output'] = generate_output |
|
149 | 149 | |
|
150 | 150 | with capture_output() as io: |
|
151 | 151 | ip.run_cell_magic('px', '--group-outputs=order', 'generate_output()') |
|
152 | 152 | |
|
153 | 153 | self.assertFalse('\n\n' in io.stdout) |
|
154 | 154 | lines = io.stdout.splitlines()[1:] |
|
155 | 155 | expected = [] |
|
156 | 156 | expected.extend([ |
|
157 | 157 | r'\[stdout:\d+\]', |
|
158 | 158 | 'stdout', |
|
159 | 159 | 'stdout2', |
|
160 | 160 | ] * len(v)) |
|
161 | 161 | expected.extend([ |
|
162 | 162 | r'\[output:\d+\]', |
|
163 | 163 | 'IPython.core.display.HTML', |
|
164 | 164 | ] * len(v)) |
|
165 | 165 | expected.extend([ |
|
166 | 166 | r'\[output:\d+\]', |
|
167 | 167 | 'IPython.core.display.Math', |
|
168 | 168 | ] * len(v)) |
|
169 | 169 | expected.extend([ |
|
170 | 170 | r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math' |
|
171 | 171 | ] * len(v)) |
|
172 | 172 | |
|
173 | 173 | self.assertEquals(len(lines), len(expected), io.stdout) |
|
174 | 174 | for line,expect in zip(lines, expected): |
|
175 | 175 | if isinstance(expect, str): |
|
176 | 176 | expect = [expect] |
|
177 | 177 | for ex in expect: |
|
178 | 178 | self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line)) |
|
179 | 179 | |
|
180 | 180 | self._check_generated_stderr(io.stderr, len(v)) |
|
181 | 181 | |
|
182 | 182 | def test_cellpx_groupby_type(self): |
|
183 | 183 | """%%px --group-outputs=type""" |
|
184 | 184 | ip = get_ipython() |
|
185 | 185 | v = self.client[:] |
|
186 | 186 | v.block = True |
|
187 | 187 | v.activate() |
|
188 | 188 | |
|
189 | 189 | v['generate_output'] = generate_output |
|
190 | 190 | |
|
191 | 191 | with capture_output() as io: |
|
192 | 192 | ip.run_cell_magic('px', '--group-outputs=type', 'generate_output()') |
|
193 | 193 | |
|
194 | 194 | self.assertFalse('\n\n' in io.stdout) |
|
195 | 195 | lines = io.stdout.splitlines()[1:] |
|
196 | 196 | |
|
197 | 197 | expected = [] |
|
198 | 198 | expected.extend([ |
|
199 | 199 | r'\[stdout:\d+\]', |
|
200 | 200 | 'stdout', |
|
201 | 201 | 'stdout2', |
|
202 | 202 | ] * len(v)) |
|
203 | 203 | expected.extend([ |
|
204 | 204 | r'\[output:\d+\]', |
|
205 | 205 | r'IPython\.core\.display\.HTML', |
|
206 | 206 | r'IPython\.core\.display\.Math', |
|
207 | 207 | ] * len(v)) |
|
208 | 208 | expected.extend([ |
|
209 | 209 | (r'Out\[\d+:\d+\]', r'IPython\.core\.display\.Math') |
|
210 | 210 | ] * len(v)) |
|
211 | 211 | |
|
212 | 212 | self.assertEquals(len(lines), len(expected), io.stdout) |
|
213 | 213 | for line,expect in zip(lines, expected): |
|
214 | 214 | if isinstance(expect, str): |
|
215 | 215 | expect = [expect] |
|
216 | 216 | for ex in expect: |
|
217 | 217 | self.assertTrue(re.search(ex, line) is not None, "Expected %r in %r" % (ex, line)) |
|
218 | 218 | |
|
219 | 219 | self._check_generated_stderr(io.stderr, len(v)) |
|
220 | 220 | |
|
221 | 221 | |
|
222 | 222 | def test_px_nonblocking(self): |
|
223 | 223 | ip = get_ipython() |
|
224 | 224 | v = self.client[-1:] |
|
225 | 225 | v.activate() |
|
226 | 226 | v.block=False |
|
227 | 227 | |
|
228 | 228 | ip.magic('px a=5') |
|
229 | 229 | self.assertEquals(v['a'], [5]) |
|
230 | 230 | ip.magic('px a=10') |
|
231 | 231 | self.assertEquals(v['a'], [10]) |
|
232 | 232 | with capture_output() as io: |
|
233 | 233 | ar = ip.magic('px print (a)') |
|
234 | 234 | self.assertTrue(isinstance(ar, AsyncResult)) |
|
235 | 235 | self.assertTrue('Async' in io.stdout) |
|
236 | 236 | self.assertFalse('[stdout:' in io.stdout) |
|
237 | 237 | self.assertFalse('\n\n' in io.stdout) |
|
238 | 238 | |
|
239 | 239 | ar = ip.magic('px 1/0') |
|
240 | 240 | self.assertRaisesRemote(ZeroDivisionError, ar.get) |
|
241 | 241 | |
|
242 | 242 | def test_autopx_blocking(self): |
|
243 | 243 | ip = get_ipython() |
|
244 | 244 | v = self.client[-1] |
|
245 | 245 | v.activate() |
|
246 | 246 | v.block=True |
|
247 | 247 | |
|
248 | 248 | with capture_output() as io: |
|
249 | 249 | ip.magic('autopx') |
|
250 | 250 | ip.run_cell('\n'.join(('a=5','b=12345','c=0'))) |
|
251 | 251 | ip.run_cell('b*=2') |
|
252 | 252 | ip.run_cell('print (b)') |
|
253 | 253 | ip.run_cell('b') |
|
254 | 254 | ip.run_cell("b/c") |
|
255 | 255 | ip.magic('autopx') |
|
256 | 256 | |
|
257 | 257 | output = io.stdout |
|
258 | 258 | |
|
259 | 259 | self.assertTrue(output.startswith('%autopx enabled'), output) |
|
260 | 260 | self.assertTrue(output.rstrip().endswith('%autopx disabled'), output) |
|
261 | 261 | self.assertTrue('ZeroDivisionError' in output, output) |
|
262 | 262 | self.assertTrue('\nOut[' in output, output) |
|
263 | 263 | self.assertTrue(': 24690' in output, output) |
|
264 | 264 | ar = v.get_result(-1) |
|
265 | 265 | self.assertEquals(v['a'], 5) |
|
266 | 266 | self.assertEquals(v['b'], 24690) |
|
267 | 267 | self.assertRaisesRemote(ZeroDivisionError, ar.get) |
|
268 | 268 | |
|
269 | 269 | def test_autopx_nonblocking(self): |
|
270 | 270 | ip = get_ipython() |
|
271 | 271 | v = self.client[-1] |
|
272 | 272 | v.activate() |
|
273 | 273 | v.block=False |
|
274 | 274 | |
|
275 | 275 | with capture_output() as io: |
|
276 | 276 | ip.magic('autopx') |
|
277 | 277 | ip.run_cell('\n'.join(('a=5','b=10','c=0'))) |
|
278 | 278 | ip.run_cell('print (b)') |
|
279 | 279 | ip.run_cell('import time; time.sleep(0.1)') |
|
280 | 280 | ip.run_cell("b/c") |
|
281 | 281 | ip.run_cell('b*=2') |
|
282 | 282 | ip.magic('autopx') |
|
283 | 283 | |
|
284 | 284 | output = io.stdout.rstrip() |
|
285 | 285 | |
|
286 | 286 | self.assertTrue(output.startswith('%autopx enabled')) |
|
287 | 287 | self.assertTrue(output.endswith('%autopx disabled')) |
|
288 | 288 | self.assertFalse('ZeroDivisionError' in output) |
|
289 | 289 | ar = v.get_result(-2) |
|
290 | 290 | self.assertRaisesRemote(ZeroDivisionError, ar.get) |
|
291 | 291 | # prevent TaskAborted on pulls, due to ZeroDivisionError |
|
292 | 292 | time.sleep(0.5) |
|
293 | 293 | self.assertEquals(v['a'], 5) |
|
294 | 294 | # b*=2 will not fire, due to abort |
|
295 | 295 | self.assertEquals(v['b'], 10) |
|
296 | 296 | |
|
297 | 297 | def test_result(self): |
|
298 | 298 | ip = get_ipython() |
|
299 | 299 | v = self.client[-1] |
|
300 | 300 | v.activate() |
|
301 | 301 | data = dict(a=111,b=222) |
|
302 | 302 | v.push(data, block=True) |
|
303 | 303 | |
|
304 | 304 | for name in ('a', 'b'): |
|
305 | 305 | ip.magic('px ' + name) |
|
306 | 306 | with capture_output() as io: |
|
307 | 307 | ip.magic('pxresult') |
|
308 | 308 | output = io.stdout |
|
309 | 309 | msg = "expected %s output to include %s, but got: %s" % \ |
|
310 | 310 | ('%pxresult', str(data[name]), output) |
|
311 | 311 | self.assertTrue(str(data[name]) in output, msg) |
|
312 | 312 | |
|
313 | 313 | @dec.skipif_not_matplotlib |
|
314 | 314 | def test_px_pylab(self): |
|
315 | 315 | """%pylab works on engines""" |
|
316 | 316 | ip = get_ipython() |
|
317 | 317 | v = self.client[-1] |
|
318 | 318 | v.block = True |
|
319 | 319 | v.activate() |
|
320 | 320 | |
|
321 | 321 | with capture_output() as io: |
|
322 | 322 | ip.magic("px %pylab inline") |
|
323 | 323 | |
|
324 | 324 | self.assertTrue("Welcome to pylab" in io.stdout, io.stdout) |
|
325 | 325 | self.assertTrue("backend_inline" in io.stdout, io.stdout) |
|
326 | 326 | |
|
327 | 327 | with capture_output() as io: |
|
328 | 328 | ip.magic("px plot(rand(100))") |
|
329 | 329 | |
|
330 | 330 | self.assertTrue('Out[' in io.stdout, io.stdout) |
|
331 | 331 | self.assertTrue('matplotlib.lines' in io.stdout, io.stdout) |
|
332 | 332 | |
|
333 | 333 | def test_pxconfig(self): |
|
334 | 334 | ip = get_ipython() |
|
335 | 335 | rc = self.client |
|
336 | 336 | v = rc.activate(-1, '_tst') |
|
337 | 337 | self.assertEquals(v.targets, rc.ids[-1]) |
|
338 | 338 | ip.magic("%pxconfig_tst -t :") |
|
339 | 339 | self.assertEquals(v.targets, rc.ids) |
|
340 | 340 | ip.magic("%pxconfig_tst --block") |
|
341 | 341 | self.assertEquals(v.block, True) |
|
342 | 342 | ip.magic("%pxconfig_tst --noblock") |
|
343 | 343 | self.assertEquals(v.block, False) |
|
344 |
|
|
|
344 | ||
|
345 | def test_cellpx_targets(self): | |
|
346 | """%%px --targets doesn't change defaults""" | |
|
347 | ip = get_ipython() | |
|
348 | rc = self.client | |
|
349 | view = rc.activate(rc.ids) | |
|
350 | self.assertEquals(view.targets, rc.ids) | |
|
351 | for cell in ("pass", "1/0"): | |
|
352 | with capture_output() as io: | |
|
353 | try: | |
|
354 | ip.run_cell_magic("px", "--targets all", cell) | |
|
355 | except pmod.RemoteError: | |
|
356 | pass | |
|
357 | self.assertTrue('engine(s): all' in io.stdout) | |
|
358 | self.assertEquals(view.targets, rc.ids) | |
|
359 | ||
|
360 | ||
|
361 | def test_cellpx_block(self): | |
|
362 | """%%px --block doesn't change default""" | |
|
363 | ip = get_ipython() | |
|
364 | rc = self.client | |
|
365 | view = rc.activate(rc.ids) | |
|
366 | view.block = False | |
|
367 | self.assertEquals(view.targets, rc.ids) | |
|
368 | for cell in ("pass", "1/0"): | |
|
369 | with capture_output() as io: | |
|
370 | try: | |
|
371 | ip.run_cell_magic("px", "--block", cell) | |
|
372 | except pmod.RemoteError: | |
|
373 | pass | |
|
374 | self.assertFalse('Async' in io.stdout) | |
|
375 | self.assertFalse(view.block) | |
|
376 | ||
|
345 | 377 |
General Comments 0
You need to be logged in to leave comments.
Login now