[Python-checkins] bpo-40387: Improve queue join() example. (GH-19724)

Raymond Hettinger webhook-mailer at python.org
Sun Apr 26 21:11:35 EDT 2020


https://github.com/python/cpython/commit/88499f15f547ccf7b15d37b0eaf51cc40bad5c39
commit: 88499f15f547ccf7b15d37b0eaf51cc40bad5c39
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-04-26T18:11:27-07:00
summary:

bpo-40387: Improve queue join() example. (GH-19724)

files:
M Doc/library/queue.rst

diff --git a/Doc/library/queue.rst b/Doc/library/queue.rst
index 2eeab5e262664..0ec5900bef5bb 100644
--- a/Doc/library/queue.rst
+++ b/Doc/library/queue.rst
@@ -190,32 +190,28 @@ fully processed by daemon consumer threads.
 
 Example of how to wait for enqueued tasks to be completed::
 
+    import threading, queue
+
+    q = queue.Queue()
+
     def worker():
         while True:
             item = q.get()
-            if item is None:
-                break
-            do_work(item)
+            print(f'Working on {item}')
+            print(f'Finished {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)
+    # turn-on the worker thread
+    threading.Thread(target=worker, daemon=True).start()
 
-    for item in source():
+    # send thirty task requests to the worker
+    for item in range(30):
         q.put(item)
+    print('All task requests sent\n', end='')
 
     # 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()
+    print('All work completed')
 
 
 SimpleQueue Objects



More information about the Python-checkins mailing list