8000 bpo-40387: Improve queue join() example. (GH-19724) (GH-19726) · python/cpython@179f22c · GitHub
[go: up one dir, main page]

Skip to content

Commit 179f22c

Browse files
bpo-40387: Improve queue join() example. (GH-19724) (GH-19726)
1 parent 882a7f4 commit 179f22c

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

Doc/library/queue.rst

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,32 +190,28 @@ fully processed by daemon consumer threads.
190190

191191
Example of how to wait for enqueued tasks to be completed::
192192

193+
import threading, queue
194+
195+
q = queue.Queue()
196+
193197
def worker():
194198
while True:
195199
item = q.get()
196-
if item is None:
197-
break
198-
do_work(item)
200+
print(f'Working on {item}')
201+
print(f'Finished {item}')
199202
q.task_done()
200203

201-
q = queue.Queue()
202-
threads = []
203-
for i in range(num_worker_threads):
204-
t = threading.Thread(target=worker)
205-
t.start()
206-
threads.append(t)
204+
# turn-on the worker thread
205+
threading.Thread(target=worker, daemon=True).start()
207206

208-
for item in source():
207+
# send thirty task requests to the worker
208+
for item in range(30):
209209
q.put(item)
210+
print('All task requests sent\n', end='')
210211

211212
# block until all tasks are done
212213
q.join()
213-
214-
# stop workers
215-
for i in range(num_worker_threads):
216-
q.put(None)
217-
for t in threads:
218-
t.join()
214+
print('All work completed')
219215

220216

221217
SimpleQueue Objects

0 commit comments

Comments
 (0)
0