File tree 1 file changed +12
-16
lines changed 1 file changed +12
-16
lines changed Original file line number Diff line number Diff line change @@ -190,32 +190,28 @@ fully processed by daemon consumer threads.
190
190
191
191
Example of how to wait for enqueued tasks to be completed::
192
192
193
+ import threading, queue
194
+
195
+ q = queue.Queue()
196
+
193
197
def worker():
194
198
while True:
195
199
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}')
199
202
q.task_done()
200
203
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()
207
206
208
- for item in source():
207
+ # send thirty task requests to the worker
208
+ for item in range(30):
209
209
q.put(item)
210
+ print('All task requests sent\n', end='')
210
211
211
212
# block until all tasks are done
212
213
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')
219
215
220
216
221
217
SimpleQueue Objects
You can’t perform that action at this time.
0 commit comments