diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-04-27 01:23:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-27 01:23:14 (GMT) |
commit | 179f22c3b786ce9baa3445923f8f9708dfa5d5b7 (patch) | |
tree | 42d1f28a2d4f88d2316f7d4ba29a841035ed6770 | |
parent | 882a7f44da08c6fb210bd9a17f80903cbca84034 (diff) | |
download | cpython-179f22c3b786ce9baa3445923f8f9708dfa5d5b7.zip cpython-179f22c3b786ce9baa3445923f8f9708dfa5d5b7.tar.gz cpython-179f22c3b786ce9baa3445923f8f9708dfa5d5b7.tar.bz2 |
bpo-40387: Improve queue join() example. (GH-19724) (GH-19726)
-rw-r--r-- | Doc/library/queue.rst | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/Doc/library/queue.rst b/Doc/library/queue.rst index 2eeab5e..0ec5900 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 |