summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-03-18 13:05:43 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-03-18 13:05:43 (GMT)
commitde311345584af1dc0fa1514d5f94bea0ea867e09 (patch)
tree2160a5ba5c70567a5b95c894314a82b099e85118 /Doc
parent926ce70066941193003068c44709ac04b16c0930 (diff)
downloadcpython-de311345584af1dc0fa1514d5f94bea0ea867e09.zip
cpython-de311345584af1dc0fa1514d5f94bea0ea867e09.tar.gz
cpython-de311345584af1dc0fa1514d5f94bea0ea867e09.tar.bz2
Issue #12155: Fix queue doc example to join threads
Use None as a sentinel to stop a worker.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/queue.rst36
1 files changed, 23 insertions, 13 deletions
diff --git a/Doc/library/queue.rst b/Doc/library/queue.rst
index 680d690..1cb0935 100644
--- a/Doc/library/queue.rst
+++ b/Doc/library/queue.rst
@@ -158,22 +158,32 @@ fully processed by daemon consumer threads.
Example of how to wait for enqueued tasks to be completed::
- def worker():
- while True:
- item = q.get()
- do_work(item)
- q.task_done()
-
- q = Queue()
- for i in range(num_worker_threads):
- t = Thread(target=worker)
- t.daemon = True
+ def worker():
+ while True:
+ item = q.get()
+ if item is None:
+ break
+ do_work(item)
+ q.task_done()
+
+ q = queue.Queue()
+ threads = []
+ for i in range(num_worker_threads):
+ t = threading.Thread(target=worker)
t.start()
+ threads.append(t)
- for item in source():
- q.put(item)
+ for item in source():
+ q.put(item)
- q.join() # block until all tasks are done
+ # block until all tasks are done
+ q.join()
+
+ # stop workers
+ for i in range(num_worker_threads):
+ q.put(None)
+ for t in threads:
+ t.join()
.. seealso::