How Python simulates code cases that implement producer consumer patterns

Source: Internet
Author: User
This article mainly describes the Python simulation to implement the producer consumer model of the relevant data, where the use of thread knowledge, queue knowledge and recycling knowledge, the need for friends can refer to the next

A detailed example of how Python simulation implements the producer consumer model

The use of python3.4 simulation to achieve a producer and consumer examples, using the knowledge of thread, queue, loop, etc., the source code is as follows:

Python code


Import Queue import time import threading import random   Q=queue. Queue (5)  #生产者 def PR ():   name=threading.current_thread (). GetName ()   print (name+ "thread start ...") for   I In range:     t=random.randint (2,9)     print (name, "Sleep time:", T)     time.sleep (t);     D= "A" +str (i)     print (name+ "Saving", I+1, "Data:", D)     #q. Put ("A" +str (i), false,2000)     q.put (d)   print (" Production finished! ")   #消费者 def Co ():   name=threading.current_thread (). GetName ()   time.sleep (1)   print (name+ "thread start ...")    while True:     print (name+ "Number of queues detected:", Q.qsize ())     T=random.randint (2,9)     print (name, "Sleep time:", T)     data=q.get ();     Print (name+ "Consumes one data:", data)     p=threading. Thread (target=pr,name= "producer") c=threading. Thread (target=co,name= "Consumer 1") c2=threading. Thread (target=co,name= "Consumer 2")  P.start () C.start () C2.start ()

In this example, the scatter fairy started 1 producer threads, 2 consumer threads, the printing effect is as follows:

Python code


Producer Thread Start ... Producer Sleep Time: 4 Consumer 1 thread start ... Consumer 1 detected the number of queues: 0 Consumers 1 Sleep time: 2 consumer 2 thread start ...  Consumer 2 detected the number of queues: 0 Consumers 2 Sleep time: 3 producers are saving 1th data: A0 producer sleep Time: 9 Consumer 1 consumption one data: A0 Consumer 1 detected the number of queues: 0 Consumers 1 Sleep time: 8 producers are saving 2nd data: A1 Producers  Sleep Time: 5 Consumer 2 consumption one data: A1 Consumer 2 detected queue number: 0 Consumer 2 Sleep time: 7 producer is saving 3rd data: A2 producer sleep Time: 8 Consumer 1 Consumption one data: A2 Consumer 1 detected queue quantity: 0 Consumer 1 Sleep time: 2 producers are saving 4th data: A3 producer sleep Time: 7 Consumer 2 consumption one data: A3 Consumer 2 detected queue quantity: 0 Consumer 2 Sleep time: 9 producer is saving 5th data: A4 producer sleep Time: 2 Consumer 1 consumption one data: A4 Fee 1 Detected queue number: 0 Consumer 1 Sleep time: 5 producer is saving 6th data: A5 producer sleep time: 5 Consumer 2 consumption one data: A5 Consumer 2 detected the number of queues: 0 Consumers 2 Sleep time: 6 producers are saving 7th data: A6 Producers Sleep Time: 7 Consumer 1 consumption one data: A6 Consumer 1 detected queue number: 0 Consumer 1 Sleep time: 7 producer is saving 8th data: A7 producer sleep Time: 3 Consumer 2 consumption one data: A7 Consumer 2 detected queue quantity: 0 Consumer 2 sleep time: 8 producers are saving 9th data: A8 producer sleep Time: 2 Consumer 1 consumption one data: A8 Consumer 1 detected queue quantity: 0 Consumer 1 Sleep time: 4 producer is saving 10th data: A9 producer sleep time: 4 Consumer 2 consumption one data: A9  Fee 2 detected queue number: 0 Consumer 2 Sleep time: 5 producer is saving 11th data: A10 producer sleep Time: 2 Consumer 1 consumption one data: A10 Consumer 1 detected the number of queues: 0 Consumers 1 Sleep time: 3 producers are saving 12th data: A11 Producer Sleep Time: 3 Consumer 2 consumption one data: A11 Consumer 2 detected queue number: 0 Consumer 2 Sleep time: 3 producers areSave 13th data: A12 producer sleep Time: 3 Consumer 1 consumption one data: A12 Consumer 1 detected queue quantity: 0 Consumer 1 Sleep time: 3 producer is saving 14th data: A13 producer sleep Time: 8 Consumer 2 consumption one data: A13 consumer 2 Number of queues detected: 0 Consumer 2 Sleep time: 7 producer is saving 15th data: A14 producer sleep Time: 3 Consumer 1 consumption one data: A14 Consumer 1 detected the number of queues: 0 Consumers 1 Sleep time: 7 producers are saving 16th data: A15 Sleep Time: 2 Consumer 2 consumption one data: A15 Consumer 2 detected queue count: 0 Consumer 2 sleep time: 9

From this example, we found that using the queue to do synchronization is very simple and convenient, in addition to the queue, there are several convenient methods:

Describe the common methods in this package:


Queue.qsize () returns the size of the queue  Queue.empty () returns True if the queue is empty, or false  Queue.full () if the queue is full, returns True, and false queue.full with MaxSize size corresponds  to Queue.get ([block[, timeout]]) Get queue, timeout wait time  queue.get_nowait () quite queue.get (False) non-blocking Queue.put (item) write queue, timeout wait time  queue.put_nowait (item) quite Queue.put (item, False) Queue.task_done () after completing a work, The Queue.task_done () function sends a signal to a queue that the task has completed queue.join () actually means waiting until the queue is empty before performing another operation

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.