In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how the Go language implements a simple producer-consumer model. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. producer-consumer model
Producer-consumer model: one module (function, etc.) is responsible for generating data, which is processed by another module (in this case, the module is generalized and can be classes, functions, coroutines, threads, processes, etc.). The module that produces data is vividly called producer, while the module that processes data is called consumer.
It is not enough to abstract the producer and consumer model. The pattern also needs to have a buffer between the producer and the consumer as an intermediary. The producer puts the data into the buffer, while the consumer takes the data out of the buffer. The approximate structure is shown in the following picture.
Suppose you want to send a courier, the general process is as follows. .
1. Seal up the express delivery-- equivalent to the data produced by the producer.
2. Give the express delivery to the delivery center-the equivalent of the producer putting the data in the buffer zone.
3. The postman takes the express delivery out of the delivery center-the same as the consumer takes the data out of the buffer zone.
From this point of view, having a buffer zone has the following benefits:
Decoupling: reducing the coupling between consumers and producers. With the express delivery center, it is not necessary to give the express delivery directly to the postman. The courier does not have any dependence on the postman. If the postman is replaced one day, it will have no effect on the courier. Assume that producers and consumers are two classes respectively. If you let the producer call a method of the consumer directly, then the producer will become dependent on the consumer (that is, coupling). If the consumer's code changes in the future, it may actually affect the producer. If both depend on a buffer, and there is no direct dependence between them, the coupling will be reduced accordingly.
Concurrency: the number of producers and consumers is not equal, can still maintain normal communication. Because the function call is synchronous (or blocked), the producer has to wait there until the consumer's method returns. If consumers are slow to process data, producers can only wait to waste time. After using the producer-consumer model, producers and consumers can be two independent concurrent agents. As soon as the producer throws the created data into the buffer zone, he can go on to produce the next data. Basically do not have to rely on the processing speed of consumers. The courier doesn't have to worry about it after he directly throws the courier to the express delivery center.
Cache: producer and consumer speed mismatch, temporary storage of data. If the courier wants to send more than one delivery at a time, then the postman can not send it, so he can temporarily store other couriers in the express delivery center. That is, producers produce data too fast in a short period of time, consumers have no time to consume, and unprocessed data can be temporarily stored in the buffer zone.
Second, Go language implementation
The most typical application of unidirectional channel is the "producer-consumer model". Channel is divided into buffered and unbuffered channel. When parameters are passed in channel, they are passed as references.
1. Unbuffered channel
The sample code is implemented as follows
Package mainimport "fmt" func producer (out chan
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.