Show More
@@ -1,7 +1,6 | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 |
"name": "" |
|
3 | "name": "BackgroundJobs" | |
4 | "signature": "sha256:481e128e553ec13e039f3e3f5e567cc3caffe391b78b9821ee883fb8770ebc82" |
|
|||
5 | }, |
|
4 | }, | |
6 | "nbformat": 3, |
|
5 | "nbformat": 3, | |
7 | "nbformat_minor": 0, |
|
6 | "nbformat_minor": 0, | |
@@ -9,17 +8,11 | |||||
9 | { |
|
8 | { | |
10 | "cells": [ |
|
9 | "cells": [ | |
11 | { |
|
10 | { | |
12 | "cell_type": "heading", |
|
|||
13 | "level": 1, |
|
|||
14 | "metadata": {}, |
|
|||
15 | "source": [ |
|
|||
16 | "Background Jobs" |
|
|||
17 | ] |
|
|||
18 | }, |
|
|||
19 | { |
|
|||
20 | "cell_type": "markdown", |
|
11 | "cell_type": "markdown", | |
21 | "metadata": {}, |
|
12 | "metadata": {}, | |
22 | "source": [ |
|
13 | "source": [ | |
|
14 | "# Simple interactive bacgkround jobs with IPython\n", | |||
|
15 | "\n", | |||
23 | "We start by loading the `backgroundjobs` library and defining a few trivial functions to illustrate things with." |
|
16 | "We start by loading the `backgroundjobs` library and defining a few trivial functions to illustrate things with." | |
24 | ] |
|
17 | ] | |
25 | }, |
|
18 | }, | |
@@ -27,7 +20,6 | |||||
27 | "cell_type": "code", |
|
20 | "cell_type": "code", | |
28 | "collapsed": false, |
|
21 | "collapsed": false, | |
29 | "input": [ |
|
22 | "input": [ | |
30 | "from __future__ import print_function\n", |
|
|||
31 | "from IPython.lib import backgroundjobs as bg\n", |
|
23 | "from IPython.lib import backgroundjobs as bg\n", | |
32 | "\n", |
|
24 | "\n", | |
33 | "import sys\n", |
|
25 | "import sys\n", | |
@@ -47,9 +39,9 | |||||
47 | "def printfunc(interval=1, reps=5):\n", |
|
39 | "def printfunc(interval=1, reps=5):\n", | |
48 | " for n in range(reps):\n", |
|
40 | " for n in range(reps):\n", | |
49 | " time.sleep(interval)\n", |
|
41 | " time.sleep(interval)\n", | |
50 |
" print |
|
42 | " print 'In the background...', n\n", | |
51 | " sys.stdout.flush()\n", |
|
43 | " sys.stdout.flush()\n", | |
52 |
" print |
|
44 | " print 'All done!'\n", | |
53 | " sys.stdout.flush()" |
|
45 | " sys.stdout.flush()" | |
54 | ], |
|
46 | ], | |
55 | "language": "python", |
|
47 | "language": "python", | |
@@ -62,8 +54,8 | |||||
62 | "metadata": {}, |
|
54 | "metadata": {}, | |
63 | "source": [ |
|
55 | "source": [ | |
64 | "Now, we can create a job manager (called simply `jobs`) and use it to submit new jobs.\n", |
|
56 | "Now, we can create a job manager (called simply `jobs`) and use it to submit new jobs.\n", | |
65 |
" |
|
57 | "\n", | |
66 | "Run the cell below and wait a few seconds for the whole thing to finish, until you see the \"All done!\" printout." |
|
58 | "Run the cell below, it will show when the jobs start. Wait a few seconds until you see the 'all done' completion message:" | |
67 | ] |
|
59 | ] | |
68 | }, |
|
60 | }, | |
69 | { |
|
61 | { | |
@@ -75,12 +67,7 | |||||
75 | "# Start a few jobs, the first one will have ID # 0\n", |
|
67 | "# Start a few jobs, the first one will have ID # 0\n", | |
76 | "jobs.new(sleepfunc, 4)\n", |
|
68 | "jobs.new(sleepfunc, 4)\n", | |
77 | "jobs.new(sleepfunc, kw={'reps':2})\n", |
|
69 | "jobs.new(sleepfunc, kw={'reps':2})\n", | |
78 |
"jobs.new('printfunc(1,3)') |
|
70 | "jobs.new('printfunc(1,3)')" | |
79 | "\n", |
|
|||
80 | "# This makes a couple of jobs which will die. Let's keep a reference to\n", |
|
|||
81 | "# them for easier traceback reporting later\n", |
|
|||
82 | "diejob1 = jobs.new(diefunc, 1)\n", |
|
|||
83 | "diejob2 = jobs.new(diefunc, 2)" |
|
|||
84 | ], |
|
71 | ], | |
85 | "language": "python", |
|
72 | "language": "python", | |
86 | "metadata": {}, |
|
73 | "metadata": {}, | |
@@ -91,13 +78,46 | |||||
91 | "text": [ |
|
78 | "text": [ | |
92 | "Starting job # 0 in a separate thread.\n", |
|
79 | "Starting job # 0 in a separate thread.\n", | |
93 | "Starting job # 2 in a separate thread.\n", |
|
80 | "Starting job # 2 in a separate thread.\n", | |
94 |
"Starting job # 3 in a separate thread.\n" |
|
81 | "Starting job # 3 in a separate thread.\n" | |
95 | "Starting job # 4 in a separate thread.\n", |
|
82 | ] | |
96 | "Starting job # 5 in a separate thread.\n" |
|
83 | }, | |
|
84 | { | |||
|
85 | "output_type": "pyout", | |||
|
86 | "prompt_number": 10, | |||
|
87 | "text": [ | |||
|
88 | "<BackgroundJob #3: printfunc(1,3)>" | |||
|
89 | ] | |||
|
90 | }, | |||
|
91 | { | |||
|
92 | "output_type": "stream", | |||
|
93 | "stream": "stdout", | |||
|
94 | "text": [ | |||
|
95 | "In the background... 0\n" | |||
|
96 | ] | |||
|
97 | }, | |||
|
98 | { | |||
|
99 | "output_type": "stream", | |||
|
100 | "stream": "stdout", | |||
|
101 | "text": [ | |||
|
102 | "In the background... 1\n" | |||
|
103 | ] | |||
|
104 | }, | |||
|
105 | { | |||
|
106 | "output_type": "stream", | |||
|
107 | "stream": "stdout", | |||
|
108 | "text": [ | |||
|
109 | "In the background... 2\n" | |||
|
110 | ] | |||
|
111 | }, | |||
|
112 | { | |||
|
113 | "output_type": "stream", | |||
|
114 | "stream": "stdout", | |||
|
115 | "text": [ | |||
|
116 | "All done!\n" | |||
97 | ] |
|
117 | ] | |
98 | } |
|
118 | } | |
99 | ], |
|
119 | ], | |
100 |
"prompt_number": |
|
120 | "prompt_number": 10 | |
101 | }, |
|
121 | }, | |
102 | { |
|
122 | { | |
103 | "cell_type": "markdown", |
|
123 | "cell_type": "markdown", | |
@@ -119,27 +139,15 | |||||
119 | "output_type": "stream", |
|
139 | "output_type": "stream", | |
120 | "stream": "stdout", |
|
140 | "stream": "stdout", | |
121 | "text": [ |
|
141 | "text": [ | |
122 | "In the background... 0\n", |
|
142 | "Completed jobs:\n", | |
123 | "Running jobs:" |
|
143 | "0 : <function sleepfunc at 0x314f848>\n", | |
124 | ] |
|
144 | "2 : <function sleepfunc at 0x314f848>\n", | |
125 | }, |
|
|||
126 | { |
|
|||
127 | "output_type": "stream", |
|
|||
128 | "stream": "stdout", |
|
|||
129 | "text": [ |
|
|||
130 | "\n", |
|
|||
131 | "0 : <function sleepfunc at 0x102cc6848>\n", |
|
|||
132 | "2 : <function sleepfunc at 0x102cc6848>\n", |
|
|||
133 | "3 : printfunc(1,3)\n", |
|
145 | "3 : printfunc(1,3)\n", | |
134 | "5 : <function diefunc at 0x102cc68c0>\n", |
|
|||
135 | "\n", |
|
|||
136 | "Dead jobs:\n", |
|
|||
137 | "4 : <function diefunc at 0x102cc68c0>\n", |
|
|||
138 | "\n" |
|
146 | "\n" | |
139 | ] |
|
147 | ] | |
140 | } |
|
148 | } | |
141 | ], |
|
149 | ], | |
142 |
"prompt_number": |
|
150 | "prompt_number": 11 | |
143 | }, |
|
151 | }, | |
144 | { |
|
152 | { | |
145 | "cell_type": "markdown", |
|
153 | "cell_type": "markdown", | |
@@ -152,14 +160,58 | |||||
152 | "cell_type": "code", |
|
160 | "cell_type": "code", | |
153 | "collapsed": false, |
|
161 | "collapsed": false, | |
154 | "input": [ |
|
162 | "input": [ | |
155 |
"jobs[0].result |
|
163 | "jobs[0].result" | |
156 | "j0 = jobs[0]\n", |
|
|||
157 | "j0.join?" |
|
|||
158 | ], |
|
164 | ], | |
159 | "language": "python", |
|
165 | "language": "python", | |
160 | "metadata": {}, |
|
166 | "metadata": {}, | |
161 |
"outputs": [ |
|
167 | "outputs": [ | |
162 | "prompt_number": 4 |
|
168 | { | |
|
169 | "output_type": "pyout", | |||
|
170 | "prompt_number": 12, | |||
|
171 | "text": [ | |||
|
172 | "{'args': (), 'interval': 4, 'kwargs': {}}" | |||
|
173 | ] | |||
|
174 | } | |||
|
175 | ], | |||
|
176 | "prompt_number": 12 | |||
|
177 | }, | |||
|
178 | { | |||
|
179 | "cell_type": "heading", | |||
|
180 | "level": 2, | |||
|
181 | "metadata": {}, | |||
|
182 | "source": [ | |||
|
183 | "Errors and tracebacks" | |||
|
184 | ] | |||
|
185 | }, | |||
|
186 | { | |||
|
187 | "cell_type": "markdown", | |||
|
188 | "metadata": {}, | |||
|
189 | "source": [ | |||
|
190 | "The jobs manager tries to help you with debugging:" | |||
|
191 | ] | |||
|
192 | }, | |||
|
193 | { | |||
|
194 | "cell_type": "code", | |||
|
195 | "collapsed": false, | |||
|
196 | "input": [ | |||
|
197 | "# This makes a couple of jobs which will die. Let's keep a reference to\n", | |||
|
198 | "# them for easier traceback reporting later\n", | |||
|
199 | "diejob1 = jobs.new(diefunc, 1)\n", | |||
|
200 | "diejob2 = jobs.new(diefunc, 2)" | |||
|
201 | ], | |||
|
202 | "language": "python", | |||
|
203 | "metadata": {}, | |||
|
204 | "outputs": [ | |||
|
205 | { | |||
|
206 | "output_type": "stream", | |||
|
207 | "stream": "stdout", | |||
|
208 | "text": [ | |||
|
209 | "Starting job # 4 in a separate thread.\n", | |||
|
210 | "Starting job # 5 in a separate thread.\n" | |||
|
211 | ] | |||
|
212 | } | |||
|
213 | ], | |||
|
214 | "prompt_number": 13 | |||
163 | }, |
|
215 | }, | |
164 | { |
|
216 | { | |
165 | "cell_type": "markdown", |
|
217 | "cell_type": "markdown", | |
@@ -184,21 +236,26 | |||||
184 | "output_type": "stream", |
|
236 | "output_type": "stream", | |
185 | "stream": "stdout", |
|
237 | "stream": "stdout", | |
186 | "text": [ |
|
238 | "text": [ | |
187 | "In the background... 1\n", |
|
239 | "Status of diejob1: Dead (Exception), call jobs.traceback() for details\n", | |
188 | "In the background... 2\n", |
|
240 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", | |
189 | "All done!\n" |
|
241 | "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n", | |
190 | ] |
|
242 | "\u001b[1;32m/home/fperez/usr/opt/virtualenv/ipython-0.13.2/lib/python2.7/site-packages/IPython/lib/backgroundjobs.pyc\u001b[0m in \u001b[0;36mcall\u001b[1;34m(self)\u001b[0m\n", | |
191 | }, |
|
243 | "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
192 | { |
|
244 | "\u001b[0;32m 483\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mcall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
193 | "ename": "SyntaxError", |
|
245 | "\u001b[1;32m--> 484\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
194 | "evalue": "invalid syntax (<ipython-input-5-a90bd59af669>, line 1)", |
|
246 | "\u001b[0m\n", | |
195 | "output_type": "pyerr", |
|
247 | "\u001b[1;32m<ipython-input-1-fbbbd0d2a1c3>\u001b[0m in \u001b[0;36mdiefunc\u001b[1;34m(interval, *a, **kw)\u001b[0m\n", | |
196 | "traceback": [ |
|
248 | "\u001b[0;32m 13\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mdiefunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
197 | "\u001b[0;36m File \u001b[0;32m\"<ipython-input-5-a90bd59af669>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print \"Status of diejob1:\", diejob1.status\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" |
|
249 | "\u001b[0;32m 14\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
|
250 | "\u001b[1;32m---> 15\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Dead job with interval %s\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0minterval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |||
|
251 | "\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |||
|
252 | "\u001b[0;32m 17\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mprintfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreps\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |||
|
253 | "\n", | |||
|
254 | "\u001b[1;31mException\u001b[0m: Dead job with interval 1\n" | |||
198 | ] |
|
255 | ] | |
199 | } |
|
256 | } | |
200 | ], |
|
257 | ], | |
201 |
"prompt_number": |
|
258 | "prompt_number": 14 | |
202 | }, |
|
259 | }, | |
203 | { |
|
260 | { | |
204 | "cell_type": "markdown", |
|
261 | "cell_type": "markdown", | |
@@ -220,44 +277,44 | |||||
220 | "output_type": "stream", |
|
277 | "output_type": "stream", | |
221 | "stream": "stdout", |
|
278 | "stream": "stdout", | |
222 | "text": [ |
|
279 | "text": [ | |
223 |
"Traceback for: <BackgroundJob #4: <function diefunc at 0x |
|
280 | "Traceback for: <BackgroundJob #4: <function diefunc at 0x314f668>>\n", | |
224 |
"\u001b[ |
|
281 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", | |
225 |
"\u001b[ |
|
282 | "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n", | |
226 |
"\u001b[ |
|
283 | "\u001b[1;32m/home/fperez/usr/opt/virtualenv/ipython-0.13.2/lib/python2.7/site-packages/IPython/lib/backgroundjobs.pyc\u001b[0m in \u001b[0;36mcall\u001b[1;34m(self)\u001b[0m\n", | |
227 |
"\u001b[ |
|
284 | "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
228 |
"\u001b[ |
|
285 | "\u001b[0;32m 483\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mcall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
229 |
"\u001b[ |
|
286 | "\u001b[1;32m--> 484\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
230 | "\u001b[0m\n", |
|
287 | "\u001b[0m\n", | |
231 |
"\u001b[ |
|
288 | "\u001b[1;32m<ipython-input-1-fbbbd0d2a1c3>\u001b[0m in \u001b[0;36mdiefunc\u001b[1;34m(interval, *a, **kw)\u001b[0m\n", | |
232 |
"\u001b[ |
|
289 | "\u001b[0;32m 13\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mdiefunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
233 |
"\u001b[ |
|
290 | "\u001b[0;32m 14\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
234 |
"\u001b[ |
|
291 | "\u001b[1;32m---> 15\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Dead job with interval %s\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0minterval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
235 |
"\u001b[0m\u001b[ |
|
292 | "\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
236 |
"\u001b[ |
|
293 | "\u001b[0;32m 17\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mprintfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreps\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
237 | "\n", |
|
294 | "\n", | |
238 |
"\u001b[ |
|
295 | "\u001b[1;31mException\u001b[0m: Dead job with interval 1\n", | |
239 | "\n", |
|
296 | "\n", | |
240 |
"Traceback for: <BackgroundJob #5: <function diefunc at 0x |
|
297 | "Traceback for: <BackgroundJob #5: <function diefunc at 0x314f668>>\n", | |
241 |
"\u001b[ |
|
298 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", | |
242 |
"\u001b[ |
|
299 | "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n", | |
243 |
"\u001b[ |
|
300 | "\u001b[1;32m/home/fperez/usr/opt/virtualenv/ipython-0.13.2/lib/python2.7/site-packages/IPython/lib/backgroundjobs.pyc\u001b[0m in \u001b[0;36mcall\u001b[1;34m(self)\u001b[0m\n", | |
244 |
"\u001b[ |
|
301 | "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
245 |
"\u001b[ |
|
302 | "\u001b[0;32m 483\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mcall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
246 |
"\u001b[ |
|
303 | "\u001b[1;32m--> 484\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
247 | "\u001b[0m\n", |
|
304 | "\u001b[0m\n", | |
248 |
"\u001b[ |
|
305 | "\u001b[1;32m<ipython-input-1-fbbbd0d2a1c3>\u001b[0m in \u001b[0;36mdiefunc\u001b[1;34m(interval, *a, **kw)\u001b[0m\n", | |
249 |
"\u001b[ |
|
306 | "\u001b[0;32m 13\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mdiefunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
250 |
"\u001b[ |
|
307 | "\u001b[0;32m 14\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
251 |
"\u001b[ |
|
308 | "\u001b[1;32m---> 15\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Dead job with interval %s\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0minterval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
252 |
"\u001b[0m\u001b[ |
|
309 | "\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
253 |
"\u001b[ |
|
310 | "\u001b[0;32m 17\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mprintfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreps\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
254 | "\n", |
|
311 | "\n", | |
255 |
"\u001b[ |
|
312 | "\u001b[1;31mException\u001b[0m: Dead job with interval 2\n", | |
256 | "\n" |
|
313 | "\n" | |
257 | ] |
|
314 | ] | |
258 | } |
|
315 | } | |
259 | ], |
|
316 | ], | |
260 |
"prompt_number": |
|
317 | "prompt_number": 15 | |
261 | }, |
|
318 | }, | |
262 | { |
|
319 | { | |
263 | "cell_type": "markdown", |
|
320 | "cell_type": "markdown", | |
@@ -284,7 +341,7 | |||||
284 | ] |
|
341 | ] | |
285 | } |
|
342 | } | |
286 | ], |
|
343 | ], | |
287 |
"prompt_number": |
|
344 | "prompt_number": 16 | |
288 | }, |
|
345 | }, | |
289 | { |
|
346 | { | |
290 | "cell_type": "markdown", |
|
347 | "cell_type": "markdown", | |
@@ -302,13 +359,13 | |||||
302 | "language": "python", |
|
359 | "language": "python", | |
303 | "metadata": {}, |
|
360 | "metadata": {}, | |
304 | "outputs": [], |
|
361 | "outputs": [], | |
305 |
"prompt_number": |
|
362 | "prompt_number": 17 | |
306 | }, |
|
363 | }, | |
307 | { |
|
364 | { | |
308 | "cell_type": "markdown", |
|
365 | "cell_type": "markdown", | |
309 | "metadata": {}, |
|
366 | "metadata": {}, | |
310 | "source": [ |
|
367 | "source": [ | |
311 | "It's easy to wait on a job:" |
|
368 | "Jobs have a `.join` method that lets you wait on their thread for completion:" | |
312 | ] |
|
369 | ] | |
313 | }, |
|
370 | }, | |
314 | { |
|
371 | { | |
@@ -316,11 +373,7 | |||||
316 | "collapsed": false, |
|
373 | "collapsed": false, | |
317 | "input": [ |
|
374 | "input": [ | |
318 | "j = jobs.new(sleepfunc, 2)\n", |
|
375 | "j = jobs.new(sleepfunc, 2)\n", | |
319 | "print(\"Will wait for j now...\")\n", |
|
376 | "j.join?" | |
320 | "sys.stdout.flush()\n", |
|
|||
321 | "j.join()\n", |
|
|||
322 | "print(\"Result from j:\")\n", |
|
|||
323 | "j.result" |
|
|||
324 | ], |
|
377 | ], | |
325 | "language": "python", |
|
378 | "language": "python", | |
326 | "metadata": {}, |
|
379 | "metadata": {}, | |
@@ -329,27 +382,22 | |||||
329 | "output_type": "stream", |
|
382 | "output_type": "stream", | |
330 | "stream": "stdout", |
|
383 | "stream": "stdout", | |
331 | "text": [ |
|
384 | "text": [ | |
332 |
"Starting job # 0 in a separate thread.\n" |
|
385 | "Starting job # 0 in a separate thread.\n" | |
333 | "Will wait for j now...\n" |
|
|||
334 | ] |
|
|||
335 | }, |
|
|||
336 | { |
|
|||
337 | "output_type": "stream", |
|
|||
338 | "stream": "stdout", |
|
|||
339 | "text": [ |
|
|||
340 | "Result from j:\n" |
|
|||
341 | ] |
|
|||
342 | }, |
|
|||
343 | { |
|
|||
344 | "metadata": {}, |
|
|||
345 | "output_type": "pyout", |
|
|||
346 | "prompt_number": 9, |
|
|||
347 | "text": [ |
|
|||
348 | "{'args': (), 'interval': 2, 'kwargs': {}}" |
|
|||
349 | ] |
|
386 | ] | |
350 | } |
|
387 | } | |
351 | ], |
|
388 | ], | |
352 |
"prompt_number": |
|
389 | "prompt_number": 18 | |
|
390 | }, | |||
|
391 | { | |||
|
392 | "cell_type": "markdown", | |||
|
393 | "metadata": {}, | |||
|
394 | "source": [ | |||
|
395 | "## Exercise\n", | |||
|
396 | "\n", | |||
|
397 | "1. Start a new job that calls `sleepfunc` with a 5-second wait\n", | |||
|
398 | "2. Print a short message that indicates you are waiting (note: you'll need to flush stdout to see that print output appear).\n", | |||
|
399 | "3. Wait on the job and then print its result." | |||
|
400 | ] | |||
353 | } |
|
401 | } | |
354 | ], |
|
402 | ], | |
355 | "metadata": {} |
|
403 | "metadata": {} |
General Comments 0
You need to be logged in to leave comments.
Login now