##// END OF EJS Templates
update Parallel Magics notebook
MinRK -
Show More
@@ -16,21 +16,19 b''
16 16 {
17 17 "cell_type": "markdown",
18 18 "source": [
19 "IPython has a few magics for working with your engines.",
20 "",
21 "This assumes you have started an IPython cluster, either with the notebook interface,",
19 "IPython has a few magics for working with your engines.\n",
20 "\n",
21 "This assumes you have started an IPython cluster, either with the notebook interface,\n",
22 22 "or the `ipcluster/controller/engine` commands."
23 23 ]
24 24 },
25 25 {
26 26 "cell_type": "code",
27 "collapsed": false,
28 27 "input": [
29 "from IPython import parallel",
30 "rc = parallel.Client()",
31 "dv = rc[:]",
32 "dv.block = True",
33 "dv"
28 "from IPython import parallel\n",
29 "rc = parallel.Client()\n",
30 "dv = rc[:]\n",
31 "rc.ids"
34 32 ],
35 33 "language": "python",
36 34 "outputs": []
@@ -38,21 +36,11 b''
38 36 {
39 37 "cell_type": "markdown",
40 38 "source": [
41 "The parallel magics come from the `parallelmagics` IPython extension.",
42 "The magics are set to work with a particular View object,",
43 "so to activate them, you call the `activate()` method on a particular view:"
39 "Creating a Client registers the parallel magics `%px`, `%%px`, `%pxresult`, and `%autopx`. \n",
40 "These magics are initially associated with a DirectView always associated with all currently registered engines."
44 41 ]
45 42 },
46 43 {
47 "cell_type": "code",
48 "collapsed": true,
49 "input": [
50 "dv.activate()"
51 ],
52 "language": "python",
53 "outputs": []
54 },
55 {
56 44 "cell_type": "markdown",
57 45 "source": [
58 46 "Now we can execute code remotely with `%px`:"
@@ -60,7 +48,6 b''
60 48 },
61 49 {
62 50 "cell_type": "code",
63 "collapsed": false,
64 51 "input": [
65 52 "%px a=5"
66 53 ],
@@ -69,7 +56,6 b''
69 56 },
70 57 {
71 58 "cell_type": "code",
72 "collapsed": false,
73 59 "input": [
74 60 "%px print a"
75 61 ],
@@ -78,7 +64,6 b''
78 64 },
79 65 {
80 66 "cell_type": "code",
81 "collapsed": false,
82 67 "input": [
83 68 "%px a"
84 69 ],
@@ -87,9 +72,8 b''
87 72 },
88 73 {
89 74 "cell_type": "code",
90 "collapsed": false,
91 75 "input": [
92 "with dv.sync_imports():",
76 "with dv.sync_imports():\n",
93 77 " import sys"
94 78 ],
95 79 "language": "python",
@@ -97,7 +81,6 b''
97 81 },
98 82 {
99 83 "cell_type": "code",
100 "collapsed": false,
101 84 "input": [
102 85 "%px print >> sys.stderr, \"ERROR\""
103 86 ],
@@ -107,24 +90,22 b''
107 90 {
108 91 "cell_type": "markdown",
109 92 "source": [
110 "You don't have to wait for results:"
93 "You don't have to wait for results. The `%pxconfig` magic lets you change the default blocking/targets for the `%px` magics:"
111 94 ]
112 95 },
113 96 {
114 97 "cell_type": "code",
115 "collapsed": true,
116 98 "input": [
117 "dv.block = False"
99 "%pxconfig --noblock"
118 100 ],
119 101 "language": "python",
120 102 "outputs": []
121 103 },
122 104 {
123 105 "cell_type": "code",
124 "collapsed": false,
125 106 "input": [
126 "%px import time",
127 "%px time.sleep(5)",
107 "%px import time\n",
108 "%px time.sleep(5)\n",
128 109 "%px time.time()"
129 110 ],
130 111 "language": "python",
@@ -133,13 +114,12 b''
133 114 {
134 115 "cell_type": "markdown",
135 116 "source": [
136 "But you will notice that this didn't output the result of the last command.",
117 "But you will notice that this didn't output the result of the last command.\n",
137 118 "For this, we have `%result`, which displays the output of the latest request:"
138 119 ]
139 120 },
140 121 {
141 122 "cell_type": "code",
142 "collapsed": false,
143 123 "input": [
144 124 "%result"
145 125 ],
@@ -154,9 +134,8 b''
154 134 },
155 135 {
156 136 "cell_type": "code",
157 "collapsed": false,
158 137 "input": [
159 "dv.block = True",
138 "%pxconfig --block\n",
160 139 "%px %pylab inline"
161 140 ],
162 141 "language": "python",
@@ -165,17 +144,15 b''
165 144 {
166 145 "cell_type": "markdown",
167 146 "source": [
168 "`%%px` can also be used as a cell magic, for submitting whole blocks.",
169 "This one acceps `--block` and `--noblock` flags to specify",
170 "the blocking behavior, though the default is unchanged.",
171 ""
147 "`%%px` can also be used as a cell magic, for submitting whole blocks.\n",
148 "This one acceps `--block` and `--noblock` flags to specify\n",
149 "the blocking behavior, though the default is unchanged.\n"
172 150 ]
173 151 },
174 152 {
175 153 "cell_type": "code",
176 "collapsed": true,
177 154 "input": [
178 "dv.scatter('id', dv.targets, flatten=True)",
155 "dv.scatter('id', dv.targets, flatten=True)\n",
179 156 "dv['stride'] = len(dv)"
180 157 ],
181 158 "language": "python",
@@ -183,13 +160,12 b''
183 160 },
184 161 {
185 162 "cell_type": "code",
186 "collapsed": false,
187 163 "input": [
188 "%%px --noblock",
189 "x = linspace(0,pi,1000)",
190 "for n in range(id,12, stride):",
191 " print n",
192 " plt.plot(x,sin(n*x))",
164 "%%px --noblock\n",
165 "x = linspace(0,pi,1000)\n",
166 "for n in range(id,12, stride):\n",
167 " print n\n",
168 " plt.plot(x,sin(n*x))\n",
193 169 "plt.title(\"Plot %i\" % id)"
194 170 ],
195 171 "language": "python",
@@ -197,7 +173,6 b''
197 173 },
198 174 {
199 175 "cell_type": "code",
200 "collapsed": false,
201 176 "input": [
202 177 "%result"
203 178 ],
@@ -207,28 +182,27 b''
207 182 {
208 183 "cell_type": "markdown",
209 184 "source": [
210 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:",
211 "",
212 "The choices are:",
213 "",
214 "* `engine` - all of an engine's output is collected together",
215 "* `type` - where stdout of each engine is grouped, etc. (the default)",
216 "* `order` - same as `type`, but individual displaypub outputs are interleaved.",
217 " That is, it will output the first plot from each engine, then the second from each,",
185 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:\n",
186 "\n",
187 "The choices are:\n",
188 "\n",
189 "* `engine` - all of an engine's output is collected together\n",
190 "* `type` - where stdout of each engine is grouped, etc. (the default)\n",
191 "* `order` - same as `type`, but individual displaypub outputs are interleaved.\n",
192 " That is, it will output the first plot from each engine, then the second from each,\n",
218 193 " etc."
219 194 ]
220 195 },
221 196 {
222 197 "cell_type": "code",
223 "collapsed": false,
224 198 "input": [
225 "%%px --group-outputs=engine",
226 "x = linspace(0,pi,1000)",
227 "for n in range(id,12, stride):",
228 " print n",
229 " plt.figure()",
230 " plt.plot(x,sin(n*x))",
231 "plt.title(\"Plot %i\" % id)"
199 "%%px --group-outputs=engine\n",
200 "x = linspace(0,pi,1000)\n",
201 "for n in range(id+1,12, stride):\n",
202 " print n\n",
203 " plt.figure()\n",
204 " plt.plot(x,sin(n*x))\n",
205 " plt.title(\"Plot %i\" % n)"
232 206 ],
233 207 "language": "python",
234 208 "outputs": []
@@ -236,20 +210,16 b''
236 210 {
237 211 "cell_type": "markdown",
238 212 "source": [
239 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved:"
213 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved.\n",
214 "\n",
215 "`%result` takes the same output-ordering arguments as `%%px`, \n",
216 "so you can view the previous result in a variety of different ways with a few sequential calls to `%result`:"
240 217 ]
241 218 },
242 219 {
243 220 "cell_type": "code",
244 "collapsed": false,
245 221 "input": [
246 "%%px --group-outputs=order",
247 "x = linspace(0,pi,1000)",
248 "for n in range(id,12, stride):",
249 " print n",
250 " plt.figure()",
251 " plt.plot(x,sin(n*x))",
252 "plt.title(\"Plot %i\" % id)"
222 "%result --group-outputs=order"
253 223 ],
254 224 "language": "python",
255 225 "outputs": []
@@ -269,53 +239,94 b''
269 239 },
270 240 {
271 241 "cell_type": "code",
272 "collapsed": true,
273 242 "input": [
274 "def generate_output():",
275 " \"\"\"function for testing output",
276 " ",
277 " publishes two outputs of each type, and returns something",
278 " \"\"\"",
279 " ",
280 " import sys,os",
281 " from IPython.core.display import display, HTML, Math",
282 " ",
283 " print \"stdout\"",
284 " print >> sys.stderr, \"stderr\"",
285 " ",
286 " display(HTML(\"<b>HTML</b>\"))",
287 " ",
288 " print \"stdout2\"",
289 " print >> sys.stderr, \"stderr2\"",
290 " ",
291 " display(Math(r\"\\alpha=\\beta\"))",
292 " ",
293 " return os.getpid()",
294 "",
243 "def generate_output():\n",
244 " \"\"\"function for testing output\n",
245 " \n",
246 " publishes two outputs of each type, and returns something\n",
247 " \"\"\"\n",
248 " \n",
249 " import sys,os\n",
250 " from IPython.core.display import display, HTML, Math\n",
251 " \n",
252 " print \"stdout\"\n",
253 " print >> sys.stderr, \"stderr\"\n",
254 " \n",
255 " display(HTML(\"<b>HTML</b>\"))\n",
256 " \n",
257 " print \"stdout2\"\n",
258 " print >> sys.stderr, \"stderr2\"\n",
259 " \n",
260 " display(Math(r\"\\alpha=\\beta\"))\n",
261 " \n",
262 " return os.getpid()\n",
263 "\n",
295 264 "dv['generate_output'] = generate_output"
296 265 ],
297 266 "language": "python",
298 267 "outputs": []
299 268 },
300 269 {
270 "cell_type": "markdown",
271 "source": [
272 "You can also have more than one set of parallel magics registered at a time.\n",
273 "\n",
274 "The `View.activate()` method takes a suffix argument, which is added to `'px'`."
275 ]
276 },
277 {
301 278 "cell_type": "code",
302 "collapsed": true,
303 279 "input": [
304 "e0 = rc[-1]",
305 "e0.block = True",
306 "e0.activate()"
280 "e0 = rc[-1]\n",
281 "e0.block = True\n",
282 "e0.activate('0')"
283 ],
284 "language": "python",
285 "outputs": []
286 },
287 {
288 "cell_type": "code",
289 "input": [
290 "%px0 generate_output()"
307 291 ],
308 292 "language": "python",
309 293 "outputs": []
310 294 },
311 295 {
312 296 "cell_type": "code",
313 "collapsed": false,
314 297 "input": [
315 298 "%px generate_output()"
316 299 ],
317 300 "language": "python",
318 301 "outputs": []
302 },
303 {
304 "cell_type": "markdown",
305 "source": [
306 "As mentioned above, we can redisplay those same results with various grouping:"
307 ]
308 },
309 {
310 "cell_type": "code",
311 "input": [
312 "%pxresult --group-outputs order"
313 ],
314 "language": "python",
315 "outputs": []
316 },
317 {
318 "cell_type": "code",
319 "input": [
320 "%pxresult --group-outputs engine"
321 ],
322 "language": "python",
323 "outputs": []
324 },
325 {
326 "cell_type": "code",
327 "input": [],
328 "language": "python",
329 "outputs": []
319 330 }
320 331 ]
321 332 }
General Comments 0
You need to be logged in to leave comments. Login now