\$\begingroup\$
\$\endgroup\$
2
Here is my code which helps me to contol the maximum amount of goroutines run simultaneously.
In example below maximum is 5.
var wg sync.WaitGroup
// Create bufferized channel with size 5
goroutines := make(chan struct{}, 5)
// Read data from input channel
for data := range input {
// 1 struct{}{} - 1 goroutine
goroutines <- struct{}{}
wg.Add(1)
go func(data string, goroutines <-chan struct{}, results chan<- result, wg *sync.WaitGroup) {
// Process data
results <- process(data)
// Read from "goroutines" channel to free space for new goroutine
<-goroutines
wg.Done()
}(data, goroutines, results, &wg)
}
wg.Wait()
close(goroutines)
Is there a better way to control the maximum amount of goroutines?
How can I improve my code?
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 27, 2017 at 12:49
1 Answer 1
\$\begingroup\$
\$\endgroup\$
You can use the Golang Concurrency Manager goccm
to limit the maximum number of goroutines to run concurrently:
Example:
package main
import (
"fmt"
"goccm"
"time"
)
func main() {
// Limit 3 goroutines to run concurrently.
c := goccm.New(3)
for i := 1; i <= 10; i++ {
// This function have to call before any goroutine
c.Wait()
go func(i int) {
fmt.Printf("Job %d is running\n", i)
time.Sleep(2 * time.Second)
// This function have to when a goroutine has finished
// Or you can use `defer c.Done()` at the top of goroutine.
c.Done()
}(i)
}
// This function have to call to ensure all goroutines have finished
// after close the main program.
c.WaitAllDone()
}
lang-golang
O(2n)
synchronizations that are not required if you would declare 5 permanent workers upfront. The runtime is built to handle millions routines, having 5 permanent workers, even sparingly consumed, is easy to handle for it and will prevent useless cpu usage. but again nothing wrong in your code. \$\endgroup\$