Sobes.tech
Senior

Do you want to write a classic worker pool?

sobes.tech AI

Answer from AI

Yes, a classic worker pool is a pattern for managing a limited number of concurrent tasks. In Go, it is usually implemented using goroutines and channels.

The idea is to have a pool of workers (goroutines) that read tasks from a channel and process them. This allows controlling the number of tasks executed simultaneously and efficiently utilizing resources.

Example of a simple worker pool in Go:

package main

import (
	"fmt"
	"sync"
)

func worker(id int, jobs <-chan int, wg *sync.WaitGroup) {
	defer wg.Done()
	for j := range jobs {
		fmt.Printf("Worker %d processing job %d\n", id, j)
	}
}

func main() {
	jobs := make(chan int, 5)
	var wg sync.WaitGroup

	// Launch 3 workers
	for w := 1; w <= 3; w++ {
		wg.Add(1)
		go worker(w, jobs, &wg)
	}

	// Send 5 tasks
	for j := 1; j <= 5; j++ {
		jobs <- j
	}
	close(jobs)

	wg.Wait()
}

This approach allows for efficient distribution of tasks among a limited number of workers.

Do you want to write a classic worker pool? — Golang - sobes.tech