My code is as follows:
import time, queue, threading
def washer(dishes, dish_queue):
for dish in dishes:
print ("Washing", dish)
time.sleep(1)
dish_queue.put(dish)
def dryer(dish_queue):
while True:
dish = dish_queue.get()
print("Drying", dish)
time.sleep(2)
dish_queue.task_done()
print('dryer')
dish_queue = queue.Queue()
for n in range(2):
dryer_thread = threading.Thread(target=dryer, args=(dish_queue,))
dryer_thread.start()
dishes = ['salad', 'bread', 'entree', 'desert']
washer(dishes, dish_queue)
dish_queue.join()
From my understanding of the documentation documentation in turn, dish_queue.join () will block the main thread until the number of unfinished tasks (unreadable dishes here) returns to 0. But I wonder what happened with 2 dry_thread.
I found that if I run the function dryeron an empty one dish_queuein the main program, the program gets stuck (BTW, is this the so-called block from dish_queue.get ()?). So if dish_queue.join()unlocks the main thread, make 2 dry_thread also unlocked and free up memory? What does a block in a queue in a doc mean ?