Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize Goroutine Pool through Channel

Shulou Source: shulou.com Published: 2022-06-02 03:57:18 09月29日 Update

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

Tags: Questions content quantity articles code situations data learning help limitations powerful useful at the same time practical actually files methods unlimited time easy to understand Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Shulou Tech Info Shulou Technology Microsoft