Queue.task_done()
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.
If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).
Raises a ValueError if called more times than there were items placed in the queue.
New in version 2.5.
Queue.join()
Blocks until all items in the queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.
# 主线程:
for thread in first_producer:
thread.set_notify_queue(queue_notify)
thread.start()
for key in range(len(first_producer)):
queue_notify.put('any thing u like, i chose string')
queue_notify.join()
# 第一组生产者线程(可能有多个)
queue_notify.get()
for ip in ip_list:
queue1.put(ip)
what_ever_other_thing()
queue_notify.task_done()
假设第一组生产者线程有20个
那么,连接第一组生产者线程和主线程的阻塞队列中,就有20个你喜欢的任意对象。由于每个生产者线程只拿一次,因此,可以保证人人有份。而在第一组生产者线程结束时,会调用一次task_done,因此,在所有第一组生产者线程结束后,主线程就能从queue_notify.join()中被唤醒,进而执行接下来的逻辑。