producer Consumer Model:
In the process of software development, often encountered such a scenario:
Some modules are responsible for production data, which is handled by other modules (the modules here may be: functions, threads, processes, etc.). The module that produces the data is called the producer, and the module that processes the data is called the consumer. The buffer between producer and consumer is called a warehouse. Producers are responsible for transporting goods to the warehouse, and consumers are responsible for taking the goods out of the warehouse, which constitutes the producer's consumer model.
Advantages of the producer consumer model
Decoupling
Assume that the producer and consumer are two threads respectively. If a producer calls a method of the consumer directly, the producer will have a dependency on the consumer (that is, coupling). If the code of the future consumer changes, it may affect the code of the producer. If both are dependent on a buffer, the two are not directly dependent, and the coupling is correspondingly reduced.
-
concurrency
since the producer and consumer are two separate concurrent bodies, they communicate with each other by buffer, and the producer can continue to produce the next data only by throwing the data into the buffer, and the consumer just takes the data from the buffer, This will not cause congestion due to the speed of each other's processing.
-
support free and busy non-uniform
When producers make data fast, the consumer is too late to deal with, the unhandled data can be temporarily in the buffer, slowly processing out. Not because of consumer performance caused by data loss or affect producer production.
example:
Import threading,time,random,queuedef product (num): "producer function, responsible for putting data into the queue" ' c = Threading. Thread (Target = customer) # Create consumer process and set as daemon, end of production, end of consumption c.setdaemon (True) # Why not Empty () judgment, because consumption is faster than production, the program will end C.start () count = 0 # counters WHILE&NBSP;COUNT&NBSP;<&NBSP;5:&NBSP;&NBSP;NAME&NBSP;=&NBSP;STR (num) +str (count) # generate different names q.put (name) # put in queue print (' Waiter {} have time! '. Format (name) #打印提示 sec = random.randint (1,5) # Random Delay time.sleep (sec) count += 1 # Add Count Def customer (): " Consumer function, extract data from the queue "' while true: sec = random.randint (1,5) time.sleep ( SEC) name = q.get () # remove data from the queue, if there is no data, block print (' waiter {} was called away! '. Format (name) Q = queue. Queue () L = []for i in range (2): p = threading. Thread (target = product, args= (i,)) l.append (P) p.start () For i in l: i.join ()
Consumer-producer Concept reference:
http://python.jobbole.com/87592/
"Python queue" producer consumer model