The producer consumer model realizes multi-threaded asynchronous interaction

Source: Internet
Author: User
Tags message queue

"Python Journey" sixth (v): the producer consumer model realizes multi-threaded asynchronous interaction

Message Queuing producer Consumer model multithreading asynchronous interaction

Abstract: Although the title is "producer consumer model for multi-threaded asynchronous interaction", but here should also include the Python message queue, because the multi-threaded asynchronous interaction is implemented through the Python message queue, so the main content is as follows: 1 2 3 4 1. Producer consumer model: Cooks make buns with customers eat buns 2.Python Message Queue 3. Use of ...

Although the title is "producer consumer model for multi-threaded asynchronous interaction", but here should also include the Python message queue, because the multi-threaded asynchronous interaction is implemented through the Python message queue, so the main content is as follows:

1234 1.生产者消费者模型:厨师做包子与顾客吃包子2.Python的消息队列3.利用消息队列实现Python多线程异步交互4.再谈耦合度的问题

1. Producer Consumer Model

Cook buns and customers eat buns to elicit producer consumer models, such as:

Here, the chef is equivalent to the producers, customers equal to consumers, customers eat steamed buns, cooks make buns. To make a hypothesis, if the chef to make steamed buns faster than customers eat steamed buns, there is such a situation, the chef and other customers to eat a bun after the next bun (then we say that the chef and the customer's high coupling, that is, cook buns and customers eat steamed buns is closely linked), so obviously efficiency will be very low, This is not possible in the real world, because once the customer is long, the chef may be too busy.

Can try to solve this: no matter whether the customer has eaten buns, cooks also continue to do buns, but at this time is to put buns in a bun counter, and so customers need to go to the bun counter to take buns. In this way, the degree of coupling between the chef and the customer becomes lower, that is, the chef does not depend on whether the customer has eaten the buns, so the efficiency will obviously be much higher.

Message Queuing in 2.Python

In the example above, you can elicit Message Queuing: The bun counter is equivalent to a message queue in Python (not only Python, of course). According to the above example, we do the following analogy analysis.

Analogy Analysis 1: cooks and buns are equivalent to two threads (assuming thread A and thread B), the Cook's bun is equivalent to the result of thread a after execution, and thread B's execution needs to take advantage of thread A's execution results, and thread a executes faster than thread B.

Analogy Analysis 2: The chef will not wait for customers to eat the bun after the next bun, that is, thread A will not wait for thread B to use the results of thread A after the execution of the next function of the same thread A2, otherwise the program will run very low efficiency.

Analogy Analysis 3: Cook put the stuffed bun in the bun counter, customers eat a bun and then go to the bun counter to take, thread A to the execution results in the message queue, and then perform the next function of the same thread A2, thread B in the message queue to win the execution of thread a results, Then perform the next function with the same thread B2, and so on.

From the above analysis, we can know that by using Message Queuing, we can reduce the coupling between two threads, so that the program can improve the efficiency of operation.

3. Using Message Queuing to implement Python multi-threaded asynchronous interaction

In the above example, thread A and thread B perform differently (asynchronously), but thread B needs to use thread A's execution result (interaction), and by using the Python message queue, the thread's asynchronous interaction can be implemented.

The program code is as follows:

1234567891011121314151617181920212223242526 #!/usr/bin/env pythonimportthreading, timeimportQueue    #导入消息队列模块importrandom   #导入随机数模块,是为了模拟生产者与消费者速度不一致的情形q = Queue.Queue()    #实例化一个对象def Producer(name):          #生产者函数    forinrange(20):          q.put(i)     #将结果放入消息队列中        print ‘\033[32;1mProducer %s has made %s baozi....\033[0m‘% (name, i)        time.sleep(random.randrange(3))    #生产者的生产速度,3s内def Consumer(name):          #消费者函数    count = 0    whilecount < 20:        data = q.get()    #取用消息队列中存放的结果        print ‘\033[31;1mConsumer %s has eatem %s baozi...chihuo...\033[0m‘% (name, data)        count += 1        time.sleep(random.randrange(4))    #消费者的消费速度,4s内 p = threading.Thread(target = Producer, args = (‘Alex‘,))c = threading.Thread(target = Consumer, args = (‘Wangfanhao‘,))p.start()c.start()

The results of the program execution are as follows:

Using this program, a good simulation of the front "cook steamed buns customers eat steamed buns" example, and from the execution results of the program can also be seen that the execution of the thread is asynchronous, however, the data is also interactive, the role is: in the multi-threaded and multi-threaded data interaction between the time, there will be no data blocking.

Based on the previous 3 points, there should be a better understanding of the Python multi-threaded asynchronous interaction.

4. Further discussion on the problem of coupling degree

Continue the front "cook steamed buns and customers to eat steamed buns" problem, there is a problem, if the production of buns is two cooks finish, that is, a stuffing, a bag of buns, then under normal circumstances, two people work needs serial completion to finish a bun, that is, the two people are very high coupling, that is, their work closely linked, In this case, we should reduce this coupling as much as possible, at this time, only need to add a queue between the two cooks, that is, cook a to do the stuffing is placed in the queue, Cook b according to the speed of their own buns to take the stuffing, so that does not appear chef a must wait for the cook b to do another stuffed bun stuffing , Chef B does not need to wait until cook a to the stuffing at hand to pack buns, because at this time there are already stuffing in the queue, so because, and then according to the two two work speed difference, to cook a or chef B to add one or more assistants, you can greatly increase the speed of the whole to do steamed buns.

In the process of writing programs, you should also think like this.

The producer consumer model realizes multi-threaded asynchronous interaction

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.