annotate Doc/lib/libqueue.tex @ 37198:bed3f52f0dc6 legacy-trunk

SF Patch #1455676: Simplify using Queues with daemon consumer threads Adds join() and task_done() methods to track when all enqueued tasks have been gotten and fully processed by daemon consumer threads.
author Raymond Hettinger <python@rcn.com>
date Fri, 24 Mar 2006 20:43:29 +0000
parents 73016bd01a71
children 06145fbc7ab9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37198
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
1
8918
acfffe14628e New section header style.
Fred Drake <fdrake@acm.org>
parents: 8716
diff changeset
2 \section{\module{Queue} ---
15142
563dee598d3a Fixed lots of small nits caught by Ka-Ping Yee <ping@lfw.org>.
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
3 A synchronized queue class}
563dee598d3a Fixed lots of small nits caught by Ka-Ping Yee <ping@lfw.org>.
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
4
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
5 \declaremodule{standard}{Queue}
8918
acfffe14628e New section header style.
Fred Drake <fdrake@acm.org>
parents: 8716
diff changeset
6 \modulesynopsis{A synchronized queue class.}
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
7
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
8
7634
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
9 The \module{Queue} module implements a multi-producer, multi-consumer
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
10 FIFO queue. It is especially useful in threads programming when
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
11 information must be exchanged safely between multiple threads. The
7634
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
12 \class{Queue} class in this module implements all the required locking
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
13 semantics. It depends on the availability of thread support in
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
14 Python.
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
15
7634
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
16 The \module{Queue} module defines the following class and exception:
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
17
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
18
7634
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
19 \begin{classdesc}{Queue}{maxsize}
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 Constructor for the class. \var{maxsize} is an integer that sets the
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
21 upperbound limit on the number of items that can be placed in the
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
22 queue. Insertion will block once this size has been reached, until
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 queue items are consumed. If \var{maxsize} is less than or equal to
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
24 zero, the queue size is infinite.
7634
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
25 \end{classdesc}
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
26
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
27 \begin{excdesc}{Empty}
10121
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
28 Exception raised when non-blocking \method{get()} (or
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
29 \method{get_nowait()}) is called on a \class{Queue} object which is
32624
73016bd01a71 Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents: 31436
diff changeset
30 empty.
10121
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
31 \end{excdesc}
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
32
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
33 \begin{excdesc}{Full}
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
34 Exception raised when non-blocking \method{put()} (or
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 10121
diff changeset
35 \method{put_nowait()}) is called on a \class{Queue} object which is
32624
73016bd01a71 Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents: 31436
diff changeset
36 full.
7634
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
37 \end{excdesc}
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
38
7634
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
39 \subsection{Queue Objects}
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
40 \label{QueueObjects}
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
41
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
42 Class \class{Queue} implements queue objects and has the methods
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
43 described below. This class can be derived from in order to implement
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
44 other queue organizations (e.g. stack) but the inheritable interface
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
45 is not described here. See the source code for details. The public
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
46 methods are:
3d1590b5e3dd Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
47
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
48 \begin{methoddesc}{qsize}{}
10121
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
49 Return the approximate size of the queue. Because of multithreading
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
50 semantics, this number is not reliable.
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
51 \end{methoddesc}
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
52
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
53 \begin{methoddesc}{empty}{}
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
54 Return \code{True} if the queue is empty, \code{False} otherwise.
32624
73016bd01a71 Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents: 31436
diff changeset
55 Because of multithreading semantics, this is not reliable.
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
56 \end{methoddesc}
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
57
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
58 \begin{methoddesc}{full}{}
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
59 Return \code{True} if the queue is full, \code{False} otherwise.
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
60 Because of multithreading semantics, this is not reliable.
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
61 \end{methoddesc}
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
62
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
63 \begin{methoddesc}{put}{item\optional{, block\optional{, timeout}}}
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
64 Put \var{item} into the queue. If optional args \var{block} is true
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
65 and \var{timeout} is None (the default), block if necessary until a
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
66 free slot is available. If \var{timeout} is a positive number, it
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
67 blocks at most \var{timeout} seconds and raises the \exception{Full}
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
68 exception if no free slot was available within that time.
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
69 Otherwise (\var{block} is false), put an item on the queue if a free
10121
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
70 slot is immediately available, else raise the \exception{Full}
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
71 exception (\var{timeout} is ignored in that case).
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
72
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
73 \versionadded[the timeout parameter]{2.3}
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
74
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
75 \end{methoddesc}
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
76
10121
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
77 \begin{methoddesc}{put_nowait}{item}
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
78 Equivalent to \code{put(\var{item}, False)}.
10121
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
79 \end{methoddesc}
b7d8125c2417 Update documentation to reflect changes to Queue.py by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8918
diff changeset
80
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
81 \begin{methoddesc}{get}{\optional{block\optional{, timeout}}}
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
82 Remove and return an item from the queue. If optional args
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
83 \var{block} is true and \var{timeout} is None (the default),
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
84 block if necessary until an item is available. If \var{timeout} is
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
85 a positive number, it blocks at most \var{timeout} seconds and raises
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
86 the \exception{Empty} exception if no item was available within that
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
87 time. Otherwise (\var{block} is false), return an item if one is
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
88 immediately available, else raise the \exception{Empty} exception
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
89 (\var{timeout} is ignored in that case).
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
90
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
91 \versionadded[the timeout parameter]{2.3}
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
92
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
93 \end{methoddesc}
6611
e6b9d21c8e0c libqueue.tex: Documentation for the Queue.py module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
94
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
95 \begin{methoddesc}{get_nowait}{}
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 23997
diff changeset
96 Equivalent to \code{get(False)}.
7768
8f177933c41f Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7634
diff changeset
97 \end{methoddesc}
37198
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
98
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
99 Two methods are offered to support tracking whether enqueued tasks have
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
100 been fully processed by daemon consumer threads.
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
101
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
102 \begin{methoddesc}{task_done}{}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
103 Indicate that a formerly enqueued task is complete. Used by queue consumer
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
104 threads. For each \method{get()} used to fetch a task, a subsequent call to
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
105 \method{task_done()} tells the queue that the processing on the task is complete.
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
106
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
107 If a \method{join()} is currently blocking, it will resume when all items
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
108 have been processed (meaning that a \method{task_done()} call was received
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
109 for every item that had been \method{put()} into the queue).
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
110
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
111 Raises a \exception{ValueError} if called more times than there were items
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
112 placed in the queue.
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
113 \versionadded{2.5}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
114 \end{methoddesc}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
115
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
116 \begin{methoddesc}{join}{}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
117 Blocks until all items in the queue have been gotten and processed.
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
118
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
119 The count of unfinished tasks goes up whenever an item is added to the
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
120 queue. The count goes down whenever a consumer thread calls \method{task_done()}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
121 to indicate that the item was retrieved and all work on it is complete.
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
122 When the count of unfinished tasks drops to zero, join() unblocks.
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
123 \versionadded{2.5}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
124 \end{methoddesc}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
125
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
126 Example of how to wait for enqueued tasks to be completed:
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
127
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
128 \begin{verbatim}
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
129 def worker():
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
130 while True:
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
131 item = q.get()
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
132 do_work(item)
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
133 q.task_done()
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
134
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
135 q = Queue()
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
136 for i in range(num_worker_threads):
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
137 t = Thread(target=worker)
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
138 t.setDaemon(True)
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
139 t.start()
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
140
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
141 for item in source():
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
142 q.put(item)
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
143
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
144 q.join() # block until all tasks are done
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 32624
diff changeset
145 \end{verbatim}