Producer Consumer mode PHP "Turn"

Source: Internet
Author: User

In the work often hear the conversation between such a Daniel will involve, XX consumer ah what, in the end what Daniel is talking about?
This article mainly solves three questions:
1. What exactly is the producer and consumer, and the story between them
2. What is the communication between them?
3. Application Scenarios

Text one, what is the producer and consumer, and the story between them

In the actual program development, will often encounter such a situation: the small a module is responsible for production data, which is handled by another module. The module that produces the data is visually called the producer, and the module that processes the data is called the consumer.

Abstract producers and consumers alone, but also not up is the producer/consumer model. The model also needs to have a buffer between the producer and the consumer as an intermediary. The producer puts the data into the buffer, and the consumer pulls the data out of the buffer. The approximate structure is like.

As you can understand, you (producers) need to write to the base friend, you put it in the mailbox (buffer), the postman (the consumer) from the mailbox to get the letter (data processing).

Then a classmate asked, good producers and consumers direct communication is not good, make a buffer is to do?
In fact, this is a connotation (merit):

Decoupling

Assuming that producers and consumers are two of classes, if the direct way for producers to adjust the consumer's class method, it is bound to rely on the consumer's class method, in case the consumer's consumption mode changes (function change) then the producers have to change. Well, the code is coupled.

So if there is a buffer is not the same, two are dependent on the buffer, buffer only play the role of buffering data, one side to take, independent and not dependent.

PS: You write to the postman directly, in case the postman changed, then you do not have to re-meet, but also care about the real postman. If it is to put the mailbox, who is the postman and you have no relationship with half a dime.

Support concurrency

Producers direct consumer a method has a disadvantage: because the function is synchronous, can also be said to be blocked, the consumer did not return before, producers can only wait, in case the consumer processing is slow, the producers idle waste.
After using the producer/consumer model, producers and consumers can be two independent concurrent principals. The producer loses the manufactured data to the buffer and can then produce the next data. Basically don't rely on the processing speed of consumers.

PS: If you do not have a mailbox, you will stand waiting for the postman, presumably this is a thing of the pit father.

Support production consumer free and busy

The buffer also has another benefit. If the speed of manufacturing data is fast and slow, the benefits of the buffer are reflected. When the data is manufactured fast, the consumer is too late to handle it, and the unhandled data can be temporarily present in the buffer. And so the producers slow down the production, consumers and then slowly dispose of.

PS: In case today is Valentine's Day what, the letter is particularly many people, the postman can only take 100, then the excess of 100 pieces can be placed in the mailbox, and so on 100 after processing good to take back, continue to deal with.

How do they communicate with each other?

So how do producers and consumers communicate? It is, of course, communicated through the data unit. What is a data unit pinch? Simply put, every time a producer puts in a buffer, it is a data unit, and every time a consumer pulls out of the buffer, it is a data unit.

PS: letter is a data unit

Then the data unit involves a very critical problem, that is, the problem of data granularity.
Sometimes, for reasons such as performance, it is possible to package N business objects into a single data unit. So, this n how to take the value is the granularity of consideration. The size of the granularity is fastidious. Too much particle size can cause some kind of waste; too small a particle size can cause performance problems. The trade-offs of granularity are based on a number of factors, as well as some empirical value considerations.

PS: Or take the example of a letter. If the particles are small (for example, set to 1), the postman only takes out 1 letters at a time. If there are more letters, you have to go back and forth a lot, wasting time.
If the particle size is too large (for example, set to 100), the person who sent the letter will have to wait until the 100 letters are filled to put it in the mailbox. If you seldom write a letter, you have to wait a long time, will be very uncomfortable drip ~

Application Scenarios

Where in PHP do you use it, or where do you see people using producer and consumer models? Here are a few simple words:

Swoole

Yes, you're right, that's swoole. So swoole actually has the pattern of using this design, the business logic (producer) of the data unit through the Swoole send function to the swoole of a buffer between, through the work process to distribute, the task process (consumer) consumption.

Asynchronous processing of time-consuming operations

When the "time-consuming operation" encounters "high Concurrency", if a bit of action is not taken, the card slows down slowly, and the information about the time-consuming operation that can be processed (the data unit) is passed through the business logic [producer]push into the Redis queue (Redis is just an example), in the process of running a script [consumer ] to process the pop out data unit.

Reproduced from

http://blog.csdn.net/u011957758/article/details/51138707

Producer Consumer mode PHP "Turn"

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.