##// END OF EJS Templates
Updating BG jobs nb from ipython-in-depth.
Brian E. Granger -
Show More
@@ -1,358 +1,406 b''
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,
8 "worksheets": [
7 "worksheets": [
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 },
26 {
19 {
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",
34 "import time\n",
26 "import time\n",
35 "\n",
27 "\n",
36 "def sleepfunc(interval=2, *a, **kw):\n",
28 "def sleepfunc(interval=2, *a, **kw):\n",
37 " args = dict(interval=interval,\n",
29 " args = dict(interval=interval,\n",
38 " args=a,\n",
30 " args=a,\n",
39 " kwargs=kw)\n",
31 " kwargs=kw)\n",
40 " time.sleep(interval)\n",
32 " time.sleep(interval)\n",
41 " return args\n",
33 " return args\n",
42 "\n",
34 "\n",
43 "def diefunc(interval=2, *a, **kw):\n",
35 "def diefunc(interval=2, *a, **kw):\n",
44 " time.sleep(interval)\n",
36 " time.sleep(interval)\n",
45 " raise Exception(\"Dead job with interval %s\" % interval)\n",
37 " raise Exception(\"Dead job with interval %s\" % interval)\n",
46 "\n",
38 "\n",
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('In the background...', n)\n",
42 " print 'In the background...', n\n",
51 " sys.stdout.flush()\n",
43 " sys.stdout.flush()\n",
52 " print('All done!')\n",
44 " print 'All done!'\n",
53 " sys.stdout.flush()"
45 " sys.stdout.flush()"
54 ],
46 ],
55 "language": "python",
47 "language": "python",
56 "metadata": {},
48 "metadata": {},
57 "outputs": [],
49 "outputs": [],
58 "prompt_number": 1
50 "prompt_number": 1
59 },
51 },
60 {
52 {
61 "cell_type": "markdown",
53 "cell_type": "markdown",
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 "<br>\n",
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 {
70 "cell_type": "code",
62 "cell_type": "code",
71 "collapsed": false,
63 "collapsed": false,
72 "input": [
64 "input": [
73 "jobs = bg.BackgroundJobManager()\n",
65 "jobs = bg.BackgroundJobManager()\n",
74 "\n",
66 "\n",
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)')\n",
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": {},
87 "outputs": [
74 "outputs": [
88 {
75 {
89 "output_type": "stream",
76 "output_type": "stream",
90 "stream": "stdout",
77 "stream": "stdout",
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": 2
120 "prompt_number": 10
101 },
121 },
102 {
122 {
103 "cell_type": "markdown",
123 "cell_type": "markdown",
104 "metadata": {},
124 "metadata": {},
105 "source": [
125 "source": [
106 "You can check the status of your jobs at any time:"
126 "You can check the status of your jobs at any time:"
107 ]
127 ]
108 },
128 },
109 {
129 {
110 "cell_type": "code",
130 "cell_type": "code",
111 "collapsed": false,
131 "collapsed": false,
112 "input": [
132 "input": [
113 "jobs.status()"
133 "jobs.status()"
114 ],
134 ],
115 "language": "python",
135 "language": "python",
116 "metadata": {},
136 "metadata": {},
117 "outputs": [
137 "outputs": [
118 {
138 {
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",
144 "2 : <function sleepfunc at 0x314f848>\n",
145 "3 : printfunc(1,3)\n",
146 "\n"
124 ]
147 ]
148 }
149 ],
150 "prompt_number": 11
125 },
151 },
126 {
152 {
127 "output_type": "stream",
153 "cell_type": "markdown",
128 "stream": "stdout",
154 "metadata": {},
155 "source": [
156 "For any completed job, you can get its result easily:"
157 ]
158 },
159 {
160 "cell_type": "code",
161 "collapsed": false,
162 "input": [
163 "jobs[0].result"
164 ],
165 "language": "python",
166 "metadata": {},
167 "outputs": [
168 {
169 "output_type": "pyout",
170 "prompt_number": 12,
129 "text": [
171 "text": [
130 "\n",
172 "{'args': (), 'interval': 4, 'kwargs': {}}"
131 "0 : <function sleepfunc at 0x102cc6848>\n",
132 "2 : <function sleepfunc at 0x102cc6848>\n",
133 "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"
139 ]
173 ]
140 }
174 }
141 ],
175 ],
142 "prompt_number": 3
176 "prompt_number": 12
177 },
178 {
179 "cell_type": "heading",
180 "level": 2,
181 "metadata": {},
182 "source": [
183 "Errors and tracebacks"
184 ]
143 },
185 },
144 {
186 {
145 "cell_type": "markdown",
187 "cell_type": "markdown",
146 "metadata": {},
188 "metadata": {},
147 "source": [
189 "source": [
148 "For any completed job, you can get its result easily:"
190 "The jobs manager tries to help you with debugging:"
149 ]
191 ]
150 },
192 },
151 {
193 {
152 "cell_type": "code",
194 "cell_type": "code",
153 "collapsed": false,
195 "collapsed": false,
154 "input": [
196 "input": [
155 "jobs[0].result\n",
197 "# This makes a couple of jobs which will die. Let's keep a reference to\n",
156 "j0 = jobs[0]\n",
198 "# them for easier traceback reporting later\n",
157 "j0.join?"
199 "diejob1 = jobs.new(diefunc, 1)\n",
200 "diejob2 = jobs.new(diefunc, 2)"
158 ],
201 ],
159 "language": "python",
202 "language": "python",
160 "metadata": {},
203 "metadata": {},
161 "outputs": [],
204 "outputs": [
162 "prompt_number": 4
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",
166 "metadata": {},
218 "metadata": {},
167 "source": [
219 "source": [
168 "You can get the traceback of any dead job. Run the line\n",
220 "You can get the traceback of any dead job. Run the line\n",
169 "below again interactively until it prints a traceback (check the status\n",
221 "below again interactively until it prints a traceback (check the status\n",
170 "of the job):\n"
222 "of the job):\n"
171 ]
223 ]
172 },
224 },
173 {
225 {
174 "cell_type": "code",
226 "cell_type": "code",
175 "collapsed": false,
227 "collapsed": false,
176 "input": [
228 "input": [
177 "print \"Status of diejob1:\", diejob1.status\n",
229 "print \"Status of diejob1:\", diejob1.status\n",
178 "diejob1.traceback() # jobs.traceback(4) would also work here, with the job number"
230 "diejob1.traceback() # jobs.traceback(4) would also work here, with the job number"
179 ],
231 ],
180 "language": "python",
232 "language": "python",
181 "metadata": {},
233 "metadata": {},
182 "outputs": [
234 "outputs": [
183 {
235 {
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": 5
258 "prompt_number": 14
202 },
259 },
203 {
260 {
204 "cell_type": "markdown",
261 "cell_type": "markdown",
205 "metadata": {},
262 "metadata": {},
206 "source": [
263 "source": [
207 "This will print all tracebacks for all dead jobs:"
264 "This will print all tracebacks for all dead jobs:"
208 ]
265 ]
209 },
266 },
210 {
267 {
211 "cell_type": "code",
268 "cell_type": "code",
212 "collapsed": false,
269 "collapsed": false,
213 "input": [
270 "input": [
214 "jobs.traceback()"
271 "jobs.traceback()"
215 ],
272 ],
216 "language": "python",
273 "language": "python",
217 "metadata": {},
274 "metadata": {},
218 "outputs": [
275 "outputs": [
219 {
276 {
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 0x102cc68c0>>\n",
280 "Traceback for: <BackgroundJob #4: <function diefunc at 0x314f668>>\n",
224 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n",
281 "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n",
225 "\u001b[0;31mException\u001b[0m Traceback (most recent call last)\n",
282 "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n",
226 "\u001b[0;32m/Users/bgranger/Documents/Computing/IPython/code/ipython/IPython/lib/backgroundjobs.pyc\u001b[0m in \u001b[0;36mcall\u001b[0;34m(self)\u001b[0m\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",
227 "\u001b[1;32m 489\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
284 "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
228 "\u001b[1;32m 490\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\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",
229 "\u001b[0;32m--> 491\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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 "\u001b[0m\n",
287 "\u001b[0m\n",
231 "\u001b[0;32m<ipython-input-1-7391f8ae281b>\u001b[0m in \u001b[0;36mdiefunc\u001b[0;34m(interval, *a, **kw)\u001b[0m\n",
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[1;32m 14\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdiefunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\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",
233 "\u001b[1;32m 15\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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",
234 "\u001b[0;32m---> 16\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Dead job with interval %s\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0minterval\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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",
235 "\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
292 "\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
236 "\u001b[1;32m 18\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mprintfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreps\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\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 "\n",
294 "\n",
238 "\u001b[0;31mException\u001b[0m: Dead job with interval 1\n",
295 "\u001b[1;31mException\u001b[0m: Dead job with interval 1\n",
239 "\n",
296 "\n",
240 "Traceback for: <BackgroundJob #5: <function diefunc at 0x102cc68c0>>\n",
297 "Traceback for: <BackgroundJob #5: <function diefunc at 0x314f668>>\n",
241 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n",
298 "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n",
242 "\u001b[0;31mException\u001b[0m Traceback (most recent call last)\n",
299 "\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n",
243 "\u001b[0;32m/Users/bgranger/Documents/Computing/IPython/code/ipython/IPython/lib/backgroundjobs.pyc\u001b[0m in \u001b[0;36mcall\u001b[0;34m(self)\u001b[0m\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",
244 "\u001b[1;32m 489\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
301 "\u001b[0;32m 482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
245 "\u001b[1;32m 490\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\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",
246 "\u001b[0;32m--> 491\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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 "\u001b[0m\n",
304 "\u001b[0m\n",
248 "\u001b[0;32m<ipython-input-1-7391f8ae281b>\u001b[0m in \u001b[0;36mdiefunc\u001b[0;34m(interval, *a, **kw)\u001b[0m\n",
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[1;32m 14\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdiefunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\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",
250 "\u001b[1;32m 15\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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",
251 "\u001b[0;32m---> 16\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Dead job with interval %s\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0minterval\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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",
252 "\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
309 "\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
253 "\u001b[1;32m 18\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mprintfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreps\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\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 "\n",
311 "\n",
255 "\u001b[0;31mException\u001b[0m: Dead job with interval 2\n",
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": 6
317 "prompt_number": 15
261 },
318 },
262 {
319 {
263 "cell_type": "markdown",
320 "cell_type": "markdown",
264 "metadata": {},
321 "metadata": {},
265 "source": [
322 "source": [
266 "The job manager can be flushed of all completed jobs at any time:"
323 "The job manager can be flushed of all completed jobs at any time:"
267 ]
324 ]
268 },
325 },
269 {
326 {
270 "cell_type": "code",
327 "cell_type": "code",
271 "collapsed": false,
328 "collapsed": false,
272 "input": [
329 "input": [
273 "jobs.flush()"
330 "jobs.flush()"
274 ],
331 ],
275 "language": "python",
332 "language": "python",
276 "metadata": {},
333 "metadata": {},
277 "outputs": [
334 "outputs": [
278 {
335 {
279 "output_type": "stream",
336 "output_type": "stream",
280 "stream": "stdout",
337 "stream": "stdout",
281 "text": [
338 "text": [
282 "Flushing 3 Completed jobs.\n",
339 "Flushing 3 Completed jobs.\n",
283 "Flushing 2 Dead jobs.\n"
340 "Flushing 2 Dead jobs.\n"
284 ]
341 ]
285 }
342 }
286 ],
343 ],
287 "prompt_number": 7
344 "prompt_number": 16
288 },
345 },
289 {
346 {
290 "cell_type": "markdown",
347 "cell_type": "markdown",
291 "metadata": {},
348 "metadata": {},
292 "source": [
349 "source": [
293 "After that, the status is simply empty:"
350 "After that, the status is simply empty:"
294 ]
351 ]
295 },
352 },
296 {
353 {
297 "cell_type": "code",
354 "cell_type": "code",
298 "collapsed": true,
355 "collapsed": true,
299 "input": [
356 "input": [
300 "jobs.status()"
357 "jobs.status()"
301 ],
358 ],
302 "language": "python",
359 "language": "python",
303 "metadata": {},
360 "metadata": {},
304 "outputs": [],
361 "outputs": [],
305 "prompt_number": 8
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 {
315 "cell_type": "code",
372 "cell_type": "code",
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": {},
327 "outputs": [
380 "outputs": [
328 {
381 {
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 ]
386 ]
387 }
388 ],
389 "prompt_number": 18
342 },
390 },
343 {
391 {
392 "cell_type": "markdown",
344 "metadata": {},
393 "metadata": {},
345 "output_type": "pyout",
394 "source": [
346 "prompt_number": 9,
395 "## Exercise\n",
347 "text": [
396 "\n",
348 "{'args': (), 'interval': 2, 'kwargs': {}}"
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."
349 ]
400 ]
350 }
401 }
351 ],
402 ],
352 "prompt_number": 9
353 }
354 ],
355 "metadata": {}
403 "metadata": {}
356 }
404 }
357 ]
405 ]
358 } No newline at end of file
406 }
General Comments 0
You need to be logged in to leave comments. Login now