The code and comments are as follows
#Auther Bob #--*--conding:utf-8 --*-- # Producer-consumer model, here's an example, there is a chef making buns, there is a customer eating buns, there is a waiter storing the buns, this waiter we can use queue to realize the import threading import queue import time ''' def consumer(p,que): id = () print("[%s] came for a bun, and the name of the bun I got was [%s]" %(p,id)) def prodcer(p,que): print("[%s] made 2 buns." %(p)) ("baozi[1]") print("baozi[1] done.") ("baozi[2]") print("baozi[2] done.") if __name__ == '__main__': que = () p = (target=prodcer,args=("Bob",que)) c1 = (target=consumer,args=("c1",que)) c2 = (target=consumer, args=("c2", que)) c3 = (target=consumer, args=("c3", que)) () () () () # () ''' # In the above example, if there are no more buns, but the chef will not know it, and the chef will not continue to make buns, and the people who don't have buns will keep waiting, and the program will never end #We can do this, the consumer realizes there are no more buns, tells the waiter, the waiter is telling the cook, and here we run into the def consumer(p): id = () print("[%s] came for a bun, and the name of the bun I got was [%s]" %(p,id)) que.task_done() #If the homing queue is empty, it will be notified and it will not block """ def prodcer(p): while True: if () < 3: # (1) for i in range(2): print("[%s] made buns [%d]" %(p,i)) (i) () # If the queue is never empty, it will always block, if the queue is empty, it will not block """ def prodcer(p): while True: # (1) for i in range(2): print("[%s] made buns [%d]" %(p,i)) (i) () # If the queue is never empty, it will always block, if the queue is empty, it will not block if __name__ == '__main__': que = () p = (target=prodcer,args=("Bob1",)) p2 = (target=prodcer, args=("Bob2",)) c1 = (target=consumer,args=("c1",)) c2 = (target=consumer, args=("c2",)) c3 = (target=consumer, args=("c3",)) c4 = (target=consumer, args=("c4",)) c5 = (target=consumer, args=("c5",)) c6 = (target=consumer, args=("c6",)) () () () () () () () () # () # que.task_done()
This is the whole content of this article.