Python queue and producer and consumer models

Source: Internet
Author: User

Queue queues

Queue thread programming is particularly useful when information must be securely exchanged between multiple threads.

queue. class Queue (maxsize=0) #先入先出
queue. class LifoQueue (maxsize=0) #last in fisrt out
queue. class PriorityQueue (maxsize=0) #存储数据时可设置优先级的队列

No data will always wait. The server side will get stuck.

If you have data, throw an exception without data.

Block=true blocking the data will block IO

Timeout = 1 blocking time 1S wait a second no new message throws an error

The maxsize= 3 queue can only hold 3 elements longer than it will get stuck later on.

Lifoqueue () after in first out
Q = queue. Lifoqueue () q.put (1) q.put (3) q.put (4) print (Q.get ()) #4print (Q.get ()) #3print (Q.get ()) #1
Priorityqueue ()Queue with Priority
Import Queueq = queue. Priorityqueue () Q.put (( -1, ' Jim ')) Q.put ((+, ' Lilei ')) Q.put ((3, ' Lily ')) Q.put ((2, ' Lucy ')) print (Q.get ()) Print (Q.get ()) Print (Q.get ()) print (Q.get ()) ############# ( -1, ' Jim ') (2, ' Lucy ') (3, ' Lily ') (+, ' Lilei ')
Producer and Consumer models

Using producer and consumer patterns in concurrent programming can solve most concurrency problems. This mode improves the overall processing speed of the program by balancing the productivity of the production line and the consuming thread.

Why use producer and consumer models

In the world of threads, the producer is the thread of production data, and the consumer is the thread of consumption data. In multithreaded development, producers have to wait for the consumer to continue producing data if the producer is processing fast and the consumer processing is slow. Similarly, consumers must wait for producers if their processing power is greater than that of producers. To solve this problem, the producer and consumer models were introduced.

What is the producer consumer model

The producer-consumer model solves the problem of strong coupling between producers and consumers through a container. Producers and consumers do not communicate with each other directly, and through the blocking queue to communicate, so producers do not have to wait for consumer processing after the production of data, directly to the blocking queue, consumers do not find producers to data, but directly from the blocking queue, the blocking queue is equivalent to a buffer, Balance the processing power of producers and consumers.

Import Queueimport threading,timeq = queue. Queue (maxsize=10) def Producer (name):    count =1    while True:        q.put ("Bun%s"% count)        print ("Made Bun", count)        Count +=1        time.sleep (0.5) def Consumer (name):    while True:        print ("[%s] takes [%s] and eats it ..."% (Name,q.get ( ))        time.sleep (1) p = Threading. Thread (target=producer,args= ("Lily",)) c = Threading. Thread (target=consumer,args= ("Lilei",)) C1 = Threading. Thread (target=consumer,args= ("Ahi",)) P.start () C.start () C1.start ()

Run results

Made buns 1[lilei] take [bun 1] and ate it ... Made buns 2[ahi] take [Bun 2] and ate it ... [Lilei] Take [bun 3] and eat it ... Made buns 3 made buns 4[ahi] take [bun 4] and ate it ... Made buns 5[lilei] take [bun 5] and ate it ... Made buns 6[ahi] take [bun 6] and ate it ... Made buns 7[lilei] take [Bun 7] and ate it ... Made buns 8[ahi] take [bun 8] and ate it ... Made buns 9[lilei] take [bun 9] and ate it ... Made buns 10[ahi] take [bun 10] and ate it ...

Python queue and producer and consumer models

Related Article

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.