How to configure goroutine limits correctly?

WBOY
Release: 2024-02-13 15:39:08
forward
600 people have browsed it

How to configure goroutine limits correctly?

php editor Yuzai will introduce you how to correctly configure goroutine restrictions. In the Go language, goroutine is a lightweight thread that can execute tasks concurrently. However, if not limited, too many goroutines may cause resource exhaustion or program crash. Therefore, it is very important to configure goroutine limits correctly. Next, we'll explore some common configuration methods to ensure your program can run stably and make full use of system resources.

Question content

I have a task that executes three functions if three functions are selected to work. I want to limit the number of horoutines per function. For example, each goroutine can only run a maximum of 10.

func main() {
    checkMethod1 := true
    checkMethod2 := false
    checkMethod3 := true

    list := []string{"info1", "info2", "info3", "info5"}

    for _, value := range list {
        value := value
        if checkMethod1 {
            go func() {
                //use value
                fmt.Println(value)
            }()
        }
        if checkMethod2 {
            go func() {
                //use value
                fmt.Println(value)
            }()
        }
        if checkMethod3 {
            go func() {
                //use value
                fmt.Println(value)
            }()
        }
    }
    //finish
    fmt.Println("All done")
}
Copy after login

I know you can limit the number of goroutines to a worker pool. However, if I create a worker pool limited to 10 goroutines, that number is divided by 3 tasks, and I need 10 goroutines per function.

I could create 3 pools, but that doesn't seem like a feasible approach to me.

I want to use this library to create a worker pool: https://github.com/sourcegraph/conc

Solution

Here is a method: use one buffer for each option Channels so you can limit active goroutines:

m1:=make(chan struct{},10)
m2:=make(chan struct{},10)
m3:=make(chan struct{},10)
wg:=sync.WaitGroup{}

for _, value := range list {
   value := value
   if checkMethod1 {
       m1<-struct{}{}
       wg.Add(1)
       go func() {
           defer func() { 
              <-m1 
              wg.Done()
           }()
           // do work
       }()
   }
   if checkMethod2 {
       m2<-struct{}{}
       wg.Add(1)
       go func() {
           defer func() { 
               <-m2 
               wg.Done()
           }()
           // do work
       }()
   }
   ...

  wg.Wait()
}
Copy after login

The above is the detailed content of How to configure goroutine limits correctly?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!