In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to achieve Goroutine Pool through Channel", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve Goroutine Pool through Channel" this article.
Recently, Go is used to import data from Excel to the server using http requests.
However, a problem was found that after reading from the file, the new Goroutine will be added without restriction.
As a result, all the cards were stuck in the initialization request, so they got stuck.
Problem simulation
Simulation code
Func main () {pool: = sync.WaitGroup {} for I: = 0500 Resp + {pool.Add (1) go func (I int) {resp, err: = http.Get ("http://ip.3322.org") if err! = nil {fmt.Println (I, err)} else {defer resp.Body.Close () result) = ioutil.ReadAll (resp.Body) fmt.Println (I, string (result))} pool.Done ()} (I)} pool.Wait ()}
There is no problem in the case of a small number, but in the case of a large number, it will be found that the program has been stuck for a period of time and no request has been made.
Problem solving
In fact, we can see that it is because too many HTTP requests are initiated at the same time that the system does not send dead data.
Thinking that I submit requests with Thread in Java, I consider whether the number of Goroutine can be limited.
Sure enough, using the powerful Baidu has found the cooperation pool that the boss has already written.
The code is as follows. I added comments.
Package gopoolimport ("sync") / / Pool Goroutine Pooltype Pool struct {queue chan int wg * sync.WaitGroup} / / New create a new protocol pool func New (size int) * Pool {if size
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.