Show More
@@ -1,358 +1,406 | |||
|
1 | 1 | { |
|
2 | 2 | "metadata": { |
|
3 |
"name": "" |
|
|
4 | "signature": "sha256:481e128e553ec13e039f3e3f5e567cc3caffe391b78b9821ee883fb8770ebc82" | |
|
3 | "name": "BackgroundJobs" | |
|
5 | 4 | }, |
|
6 | 5 | "nbformat": 3, |
|
7 | 6 | "nbformat_minor": 0, |
|
8 | 7 | "worksheets": [ |
|
9 | 8 | { |
|
10 | 9 | "cells": [ |
|
11 | 10 | { |
|
12 | "cell_type": "heading", | |
|
13 | "level": 1, | |
|
14 | "metadata": {}, | |
|
15 | "source": [ | |
|
16 | "Background Jobs" | |
|
17 | ] | |
|
18 | }, | |
|
19 | { | |
|
20 | 11 | "cell_type": "markdown", |
|
21 | 12 | "metadata": {}, |
|
22 | 13 | "source": [ |
|
14 | "# Simple interactive bacgkround jobs with IPython\n", | |
|
15 | "\n", | |
|
23 | 16 | "We start by loading the `backgroundjobs` library and defining a few trivial functions to illustrate things with." |
|
24 | 17 | ] |
|
25 | 18 | }, |
|
26 | 19 | { |
|
27 | 20 | "cell_type": "code", |
|
28 | 21 | "collapsed": false, |
|
29 | 22 | "input": [ |
|
30 | "from __future__ import print_function\n", | |
|
31 | 23 | "from IPython.lib import backgroundjobs as bg\n", |
|
32 | 24 | "\n", |
|
33 | 25 | "import sys\n", |
|
34 | 26 | "import time\n", |
|
35 | 27 | "\n", |
|
36 | 28 | "def sleepfunc(interval=2, *a, **kw):\n", |
|
37 | 29 | " args = dict(interval=interval,\n", |
|
38 | 30 | " args=a,\n", |
|
39 | 31 | " kwargs=kw)\n", |
|
40 | 32 | " time.sleep(interval)\n", |
|
41 | 33 | " return args\n", |
|
42 | 34 | "\n", |
|
43 | 35 | "def diefunc(interval=2, *a, **kw):\n", |
|
44 | 36 | " time.sleep(interval)\n", |
|
45 | 37 | " raise Exception(\"Dead job with interval %s\" % interval)\n", |
|
46 | 38 | "\n", |
|
47 | 39 | "def printfunc(interval=1, reps=5):\n", |
|
48 | 40 | " for n in range(reps):\n", |
|
49 | 41 | " time.sleep(interval)\n", |
|
50 |
" print |
|
|
42 | " print 'In the background...', n\n", | |
|
51 | 43 | " sys.stdout.flush()\n", |
|
52 |
" print |
|
|
44 | " print 'All done!'\n", | |
|
53 | 45 | " sys.stdout.flush()" |
|
54 | 46 | ], |
|
55 | 47 | "language": "python", |
|
56 | 48 | "metadata": {}, |
|
57 | 49 | "outputs": [], |
|
58 | 50 | "prompt_number": 1 |
|
59 | 51 | }, |
|
60 | 52 | { |
|
61 | 53 | "cell_type": "markdown", |
|
62 | 54 | "metadata": {}, |
|
63 | 55 | "source": [ |
|
64 | 56 | "Now, we can create a job manager (called simply `jobs`) and use it to submit new jobs.\n", |
|
65 |
" |
|
|
66 | "Run the cell below and wait a few seconds for the whole thing to finish, until you see the \"All done!\" printout." | |
|
57 | "\n", | |
|
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 | { |
|
70 | 62 | "cell_type": "code", |
|
71 | 63 | "collapsed": false, |
|
72 | 64 | "input": [ |
|
73 | 65 | "jobs = bg.BackgroundJobManager()\n", |
|
74 | 66 | "\n", |
|
75 | 67 | "# Start a few jobs, the first one will have ID # 0\n", |
|
76 | 68 | "jobs.new(sleepfunc, 4)\n", |
|
77 | 69 | "jobs.new(sleepfunc, kw={'reps':2})\n", |
|
78 |
"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)" | |
|
70 | "jobs.new('printfunc(1,3)')" | |
|
84 | 71 | ], |
|
85 | 72 | "language": "python", |
|
86 | 73 | "metadata": {}, |
|
87 | 74 | "outputs": [ |
|
88 | 75 | { |
|
89 | 76 | "output_type": "stream", |
|
90 | 77 | "stream": "stdout", |
|
91 | 78 | "text": [ |
|
92 | 79 | "Starting job # 0 in a separate thread.\n", |
|
93 | 80 | "Starting job # 2 in a separate thread.\n", |
|
94 |
"Starting job # 3 in a separate thread.\n" |
|
|
95 | "Starting job # 4 in a separate thread.\n", | |
|
96 | "Starting job # 5 in a separate thread.\n" | |
|
81 | "Starting job # 3 in a separate thread.\n" | |
|
82 | ] | |
|
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 | 123 | "cell_type": "markdown", |
|
104 | 124 | "metadata": {}, |
|
105 | 125 | "source": [ |
|
106 | 126 | "You can check the status of your jobs at any time:" |
|
107 | 127 | ] |
|
108 | 128 | }, |
|
109 | 129 | { |
|
110 | 130 | "cell_type": "code", |
|
111 | 131 | "collapsed": false, |
|
112 | 132 | "input": [ |
|
113 | 133 | "jobs.status()" |
|
114 | 134 | ], |
|
115 | 135 | "language": "python", |
|
116 | 136 | "metadata": {}, |
|
117 | 137 | "outputs": [ |
|
118 | 138 | { |
|
119 | 139 | "output_type": "stream", |
|
120 | 140 | "stream": "stdout", |
|
121 | 141 | "text": [ |
|
122 | "In the background... 0\n", | |
|
123 | "Running jobs:" | |
|
124 | ] | |
|
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", | |
|
142 | "Completed jobs:\n", | |
|
143 | "0 : <function sleepfunc at 0x314f848>\n", | |
|
144 | "2 : <function sleepfunc at 0x314f848>\n", | |
|
133 | 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 | 146 | "\n" |
|
139 | 147 | ] |
|
140 | 148 | } |
|
141 | 149 | ], |
|
142 |
"prompt_number": |
|
|
150 | "prompt_number": 11 | |
|
143 | 151 | }, |
|
144 | 152 | { |
|
145 | 153 | "cell_type": "markdown", |
|
146 | 154 | "metadata": {}, |
|
147 | 155 | "source": [ |
|
148 | 156 | "For any completed job, you can get its result easily:" |
|
149 | 157 | ] |
|
150 | 158 | }, |
|
151 | 159 | { |
|
152 | 160 | "cell_type": "code", |
|
153 | 161 | "collapsed": false, |
|
154 | 162 | "input": [ |
|
155 |
"jobs[0].result |
|
|
156 | "j0 = jobs[0]\n", | |
|
157 | "j0.join?" | |
|
163 | "jobs[0].result" | |
|
158 | 164 | ], |
|
159 | 165 | "language": "python", |
|
160 | 166 | "metadata": {}, |
|
161 |
"outputs": [ |
|
|
162 | "prompt_number": 4 | |
|
167 | "outputs": [ | |
|
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 | 217 | "cell_type": "markdown", |
|
166 | 218 | "metadata": {}, |
|
167 | 219 | "source": [ |
|
168 | 220 | "You can get the traceback of any dead job. Run the line\n", |
|
169 | 221 | "below again interactively until it prints a traceback (check the status\n", |
|
170 | 222 | "of the job):\n" |
|
171 | 223 | ] |
|
172 | 224 | }, |
|
173 | 225 | { |
|
174 | 226 | "cell_type": "code", |
|
175 | 227 | "collapsed": false, |
|
176 | 228 | "input": [ |
|
177 | 229 | "print \"Status of diejob1:\", diejob1.status\n", |
|
178 | 230 | "diejob1.traceback() # jobs.traceback(4) would also work here, with the job number" |
|
179 | 231 | ], |
|
180 | 232 | "language": "python", |
|
181 | 233 | "metadata": {}, |
|
182 | 234 | "outputs": [ |
|
183 | 235 | { |
|
184 | 236 | "output_type": "stream", |
|
185 | 237 | "stream": "stdout", |
|
186 | 238 | "text": [ |
|
187 | "In the background... 1\n", | |
|
188 | "In the background... 2\n", | |
|
189 | "All done!\n" | |
|
190 | ] | |
|
191 | }, | |
|
192 | { | |
|
193 | "ename": "SyntaxError", | |
|
194 | "evalue": "invalid syntax (<ipython-input-5-a90bd59af669>, line 1)", | |
|
195 | "output_type": "pyerr", | |
|
196 | "traceback": [ | |
|
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" | |
|
239 | "Status of diejob1: Dead (Exception), call jobs.traceback() for details\n", | |
|
240 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", | |
|
241 | "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n", | |
|
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", | |
|
243 | "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
|
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", | |
|
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", | |
|
246 | "\u001b[0m\n", | |
|
247 | "\u001b[1;32m<ipython-input-1-fbbbd0d2a1c3>\u001b[0m in \u001b[0;36mdiefunc\u001b[1;34m(interval, *a, **kw)\u001b[0m\n", | |
|
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", | |
|
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 | 261 | "cell_type": "markdown", |
|
205 | 262 | "metadata": {}, |
|
206 | 263 | "source": [ |
|
207 | 264 | "This will print all tracebacks for all dead jobs:" |
|
208 | 265 | ] |
|
209 | 266 | }, |
|
210 | 267 | { |
|
211 | 268 | "cell_type": "code", |
|
212 | 269 | "collapsed": false, |
|
213 | 270 | "input": [ |
|
214 | 271 | "jobs.traceback()" |
|
215 | 272 | ], |
|
216 | 273 | "language": "python", |
|
217 | 274 | "metadata": {}, |
|
218 | 275 | "outputs": [ |
|
219 | 276 | { |
|
220 | 277 | "output_type": "stream", |
|
221 | 278 | "stream": "stdout", |
|
222 | 279 | "text": [ |
|
223 |
"Traceback for: <BackgroundJob #4: <function diefunc at 0x |
|
|
224 |
"\u001b[ |
|
|
225 |
"\u001b[ |
|
|
226 |
"\u001b[ |
|
|
227 |
"\u001b[ |
|
|
228 |
"\u001b[ |
|
|
229 |
"\u001b[ |
|
|
280 | "Traceback for: <BackgroundJob #4: <function diefunc at 0x314f668>>\n", | |
|
281 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", | |
|
282 | "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n", | |
|
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", | |
|
284 | "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
|
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", | |
|
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 | 287 | "\u001b[0m\n", |
|
231 |
"\u001b[ |
|
|
232 |
"\u001b[ |
|
|
233 |
"\u001b[ |
|
|
234 |
"\u001b[ |
|
|
235 |
"\u001b[0m\u001b[ |
|
|
236 |
"\u001b[ |
|
|
288 | "\u001b[1;32m<ipython-input-1-fbbbd0d2a1c3>\u001b[0m in \u001b[0;36mdiefunc\u001b[1;34m(interval, *a, **kw)\u001b[0m\n", | |
|
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", | |
|
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", | |
|
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", | |
|
292 | "\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
|
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 | 294 | "\n", |
|
238 |
"\u001b[ |
|
|
295 | "\u001b[1;31mException\u001b[0m: Dead job with interval 1\n", | |
|
239 | 296 | "\n", |
|
240 |
"Traceback for: <BackgroundJob #5: <function diefunc at 0x |
|
|
241 |
"\u001b[ |
|
|
242 |
"\u001b[ |
|
|
243 |
"\u001b[ |
|
|
244 |
"\u001b[ |
|
|
245 |
"\u001b[ |
|
|
246 |
"\u001b[ |
|
|
297 | "Traceback for: <BackgroundJob #5: <function diefunc at 0x314f668>>\n", | |
|
298 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", | |
|
299 | "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n", | |
|
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", | |
|
301 | "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
|
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", | |
|
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 | 304 | "\u001b[0m\n", |
|
248 |
"\u001b[ |
|
|
249 |
"\u001b[ |
|
|
250 |
"\u001b[ |
|
|
251 |
"\u001b[ |
|
|
252 |
"\u001b[0m\u001b[ |
|
|
253 |
"\u001b[ |
|
|
305 | "\u001b[1;32m<ipython-input-1-fbbbd0d2a1c3>\u001b[0m in \u001b[0;36mdiefunc\u001b[1;34m(interval, *a, **kw)\u001b[0m\n", | |
|
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", | |
|
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", | |
|
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", | |
|
309 | "\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
|
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 | 311 | "\n", |
|
255 |
"\u001b[ |
|
|
312 | "\u001b[1;31mException\u001b[0m: Dead job with interval 2\n", | |
|
256 | 313 | "\n" |
|
257 | 314 | ] |
|
258 | 315 | } |
|
259 | 316 | ], |
|
260 |
"prompt_number": |
|
|
317 | "prompt_number": 15 | |
|
261 | 318 | }, |
|
262 | 319 | { |
|
263 | 320 | "cell_type": "markdown", |
|
264 | 321 | "metadata": {}, |
|
265 | 322 | "source": [ |
|
266 | 323 | "The job manager can be flushed of all completed jobs at any time:" |
|
267 | 324 | ] |
|
268 | 325 | }, |
|
269 | 326 | { |
|
270 | 327 | "cell_type": "code", |
|
271 | 328 | "collapsed": false, |
|
272 | 329 | "input": [ |
|
273 | 330 | "jobs.flush()" |
|
274 | 331 | ], |
|
275 | 332 | "language": "python", |
|
276 | 333 | "metadata": {}, |
|
277 | 334 | "outputs": [ |
|
278 | 335 | { |
|
279 | 336 | "output_type": "stream", |
|
280 | 337 | "stream": "stdout", |
|
281 | 338 | "text": [ |
|
282 | 339 | "Flushing 3 Completed jobs.\n", |
|
283 | 340 | "Flushing 2 Dead jobs.\n" |
|
284 | 341 | ] |
|
285 | 342 | } |
|
286 | 343 | ], |
|
287 |
"prompt_number": |
|
|
344 | "prompt_number": 16 | |
|
288 | 345 | }, |
|
289 | 346 | { |
|
290 | 347 | "cell_type": "markdown", |
|
291 | 348 | "metadata": {}, |
|
292 | 349 | "source": [ |
|
293 | 350 | "After that, the status is simply empty:" |
|
294 | 351 | ] |
|
295 | 352 | }, |
|
296 | 353 | { |
|
297 | 354 | "cell_type": "code", |
|
298 | 355 | "collapsed": true, |
|
299 | 356 | "input": [ |
|
300 | 357 | "jobs.status()" |
|
301 | 358 | ], |
|
302 | 359 | "language": "python", |
|
303 | 360 | "metadata": {}, |
|
304 | 361 | "outputs": [], |
|
305 |
"prompt_number": |
|
|
362 | "prompt_number": 17 | |
|
306 | 363 | }, |
|
307 | 364 | { |
|
308 | 365 | "cell_type": "markdown", |
|
309 | 366 | "metadata": {}, |
|
310 | 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 | { |
|
315 | 372 | "cell_type": "code", |
|
316 | 373 | "collapsed": false, |
|
317 | 374 | "input": [ |
|
318 | 375 | "j = jobs.new(sleepfunc, 2)\n", |
|
319 | "print(\"Will wait for j now...\")\n", | |
|
320 | "sys.stdout.flush()\n", | |
|
321 | "j.join()\n", | |
|
322 | "print(\"Result from j:\")\n", | |
|
323 | "j.result" | |
|
376 | "j.join?" | |
|
324 | 377 | ], |
|
325 | 378 | "language": "python", |
|
326 | 379 | "metadata": {}, |
|
327 | 380 | "outputs": [ |
|
328 | 381 | { |
|
329 | 382 | "output_type": "stream", |
|
330 | 383 | "stream": "stdout", |
|
331 | 384 | "text": [ |
|
332 |
"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': {}}" | |
|
385 | "Starting job # 0 in a separate thread.\n" | |
|
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 | 403 | "metadata": {} |
|
356 | 404 | } |
|
357 | 405 | ] |
|
358 | 406 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now