How can you efficiently launch and manage a large number of goroutines, for example, around 100,000?
sobes.tech AI
Answer from AI
For effectively launching and managing around 100,000 goroutines in Go, it is important to consider several aspects:
-
Using a worker pool — instead of launching all goroutines at once, create a fixed-size pool (for example, a few hundred), and distribute tasks among them. This reduces the load on the scheduler and decreases memory consumption.
-
Resource control — goroutines are lightweight, but each uses a stack (by default a few kilobytes), so it is important to avoid launching too many simultaneously without control.
-
Using channels for synchronization — to manage the lifecycle of goroutines and pass data between them.
-
Context (context.Context) — for cancellation and timeouts.
Example of a simple worker pool:
package main
import (
"fmt"
"sync"
)
func worker(id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
// Process task
results <- j * 2
}
}
func main() {
const numJobs = 100000
const numWorkers = 100
jobs := make(chan int, numJobs)
results := make(chan int, numJobs)
var wg sync.WaitGroup
for w := 1; w <= numWorkers; w++ {
wg.Add(1)
go worker(w, jobs, results, &wg)
}
for j := 1; j <= numJobs; j++ {
jobs <- j
}
close(jobs)
wg.Wait()
close(results)
// Process results
count := 0
for r := range results {
_ = r // use the result
count++
}
fmt.Println("Number of tasks processed:", count)
}
This approach allows efficient management of a large number of tasks using a limited number of goroutines.