"Python queue" producer consumer model

Source: Internet
Author: User

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 COUNT < 5:  NAME = 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.