Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-40387 Improve queue join() example. #19724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions Doc/library/queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down