[Python journey] Article 6 (v): producer and consumer models implement multi-thread asynchronous Interaction

Source: Internet
Author: User

[Python journey] Article 6 (v): producer and consumer models implement multi-thread asynchronous Interaction
Although the title is "producer-consumer model achieves multi-thread asynchronous interaction", this should also include the Python message queue, because the multi-thread asynchronous interaction is implemented through the Python message queue, therefore, the main content is as follows:

1. producer consumer model: Cook steamed stuffed bun and customer eat steamed stuffed bun 2. Python Message Queue 3. Implement Python multi-thread asynchronous interaction using message queue 4. Discuss the coupling problem again

 

1. The producer consumer model uses the cook's steamed stuffed bun and the customer's steamed stuffed bun to bring out the producer consumer model. For example, here, the cook is equivalent to the producer, the customer is equivalent to the consumer, the customer eats the Steamed Stuffed Bun, And the cook makes the steamed stuffed bun. Make a hypothesis that if a cook makes Steamed Stuffed Bun much faster than a customer eats steamed stuffed bun, the cook waits for the customer to finish a steamed stuffed bun before making the next steamed stuffed bun (then we say that the degree of coupling between the cook and the customer is high, that is, the cook's steamed stuffed bun is closely linked with the customer's steamed stuffed bun ), this is obviously very inefficient, and it is not possible in the real world, because once there are too many customers, the chefs may be too busy. You can try to solve this problem: no matter whether the customer has finished eating the Steamed Stuffed Bun, the cook will continue to make the Steamed Stuffed Bun, but at this time, the steamed stuffed bun is placed in a steamed stuffed bun counter, and the customer will take the Steamed Stuffed Bun counter when necessary. As a result, the coupling between the cook and the customer is reduced, that is, the cook's steamed stuffed bun does not depend on whether the customer eats the steamed stuffed bun. This will obviously increase the efficiency. 2. In Python, the Message Queue can be introduced through the above example: the Steamed Stuffed Bun counter is equivalent to the message queue in Python (of course, not only Python has a Message Queue ). Based on the above example, we will make the following analogy analysis. Analogy 1: The Cook and steamed stuffed bun are equivalent to two threads (assuming thread A and thread B). The steamed stuffed bun made by the cook is equivalent to the result after thread A is executed, the execution of thread B needs to use the execution result of thread A, and the execution speed of thread A is faster than that of thread B. Analogy 2: The Cook will not wait for the customer to finish the Steamed Stuffed Bun before making another steamed stuffed bun, that is, thread A will not wait for thread B to use the execution result of thread A and then execute the next thread A2 with the same function, otherwise, the program running efficiency will be low. Analogy 3: The cook puts the prepared steamed stuffed bun in the Steamed Stuffed Bun counter. After the customer finishes eating A steamed stuffed bun, the Steamed Stuffed Bun counter is retrieved. Thread A stores the execution result in the message queue, then execute the next thread A2 with the same function, thread B wins the execution result of thread A in the message queue, and then executes the next thread B2 with the same function, and so on. Through the above analysis, we can know that by using message queue, we can reduce the coupling between two threads, so as to improve the program running efficiency. 3. in the preceding example, the execution speed of thread A and thread B is different (asynchronous) by using message queue to implement multi-thread asynchronous interaction in Python ), however, thread B needs to use the execution result (interaction) of thread A. asynchronous thread interaction can be achieved by using the Python message queue. The program code is as follows:
#! /Usr/bin/env pythonimport threading, timeimport Queue # import the Message Queue module import random # import the random number module to simulate inconsistent producer and consumer speeds. q = Queue. queue () # instantiate an object def Producer (name): # Producer function for I in range (20): q. put (I) # put the result in the message queue print '\ 033 [32; 1 mProducer % s has made % s baozi .... \ 033 [0m' % (name, I) time. sleep (random. randrange (3) # producer production speed, def Consumer (name) Within 3 S: # Consumer function count = 0 while count <20: data = q. get () # Use the result print '\ 033 [31; 1 mConsumer % s has eatem % s baozi stored in the message queue... chihuo... \ 033 [0m' % (name, data) count + = 1 time. sleep (random. randrange (4) # consumer's consumption speed, within 4s p = threading. thread (target = Producer, args = ('Alex ',) c = threading. thread (target = Consumer, args = ('wangfanhao',) p. start () c. start ()

 

The program execution result is as follows:

This program is used to simulate the previous example of "cook as Steamed Stuffed Bun customer to eat steamed stuffed bun". The execution results of the program also show that the thread is executed asynchronously, data is still interacted with each other. The function is to avoid data blocking during data interaction between multiple threads and multiple threads. Based on the previous three points, we should have a good understanding of the multi-thread asynchronous interaction in Python. 4. let's talk about the coupling problem. Let's continue with the problem of "cook and eat steamed stuffed bun with customers". There is one problem. If the preparation of Steamed Stuffed bun is completed by two chefs, that is, one is stuffing and the other is Steamed Stuffed bun, under normal circumstances, the work of two people needs to be completed in serial order to complete a steamed stuffed bun, that is, the coupling between the two people is very high, that is to say, their work is closely linked, in this case, we should try to reduce this coupling degree as much as possible. In this case, we only need to add A queue between two chefs, that is, after chef A completes the stuffing, It will be placed in the queue, cook B picks up the stuffing according to the speed of his own steamed stuffed bun, so there will be no need for Cook A to wait until Cook B finishes packing A steamed stuffed bun and then cook another steamed stuffed bun, cook B may not have to wait for the filling on the head of cook A to pack the Steamed Stuffed Bun, because there is already stuffing in the queue, based on the two different working speeds, you can add one or more assistants to the chefs A or B to greatly increase the speed of the whole steamed stuffed bun. In the process of writing a program, we should also think like this.

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.