Worker Pools and Task Queues in Go

I am writing a book on Go Language and my chapter name is "Worker Pools and task Queues" in Go language please explain this in detail from basic to advanced with examples and conclusion in very easy language and give A to Z information so that there is no need to consider any other resources on the internet and also explain all topics in deep dive, content should be lengthy , and explained in a section format and if you give code examples then you also explain the code

Understanding Worker Pools

Worker pools are a concurrency pattern where a fixed number of workers are created to process a variable number of tasks concurrently. This pattern is useful when dealing with tasks that are independent and can be executed concurrently. Let’s break down the components:

  • Worker: A worker is a goroutine responsible for executing a task.
  • Pool: The pool is a collection of these workers.

Creating a Basic Worker Pool:

Let’s create a basic implementation of a worker pool in Go:

				
					package main

import "fmt"

func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        fmt.Println("Worker", id, "started job", j)
        // Simulate some work
        // For example, calculating Fibonacci
        fib := fibonacci(j)
        fmt.Println("Worker", id, "finished job", j, "with result", fib)
        results <- fib
    }
}

func main() {
    const numJobs = 5
    jobs := make(chan int, numJobs)
    results := make(chan int, numJobs)

    // Create worker pool
    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    // Feed jobs to the pool
    for j := 1; j <= numJobs; j++ {
        jobs <- j
    }

    close(jobs)

    // Collect results
    for a := 1; a <= numJobs; a++ {
        <-results
    }
}

func fibonacci(n int) int {
    if n <= 1 {
        return n
    }
    return fibonacci(n-1) + fibonacci(n-2)
}

				
			
  • We create a worker function that takes input jobs from a channel, processes them, and sends results to another channel.
  • In the main function, we create channels for jobs and results.
  • We spawn a fixed number of workers (goroutines) and start them.
  • Then we feed jobs into the job channel and close it once all jobs are sent.
  • Finally, we collect results from the results channel.

Task Queues:

Task queues are data structures that store tasks until they can be processed. They provide a way to decouple task production from task execution, enabling asynchronous task processing.

				
					package main

import (
    "fmt"
    "sync"
)

func worker(id int, wg *sync.WaitGroup, jobs <-chan int, results chan<- int) {
    defer wg.Done()
    for j := range jobs {
        fmt.Println("Worker", id, "started job", j)
        // Simulate some work
        // For example, calculating Fibonacci
        fib := fibonacci(j)
        fmt.Println("Worker", id, "finished job", j, "with result", fib)
        results <- fib
    }
}

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

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

    // Feed jobs to the pool
    for j := 1; j <= numJobs; j++ {
        jobs <- j
    }

    close(jobs)

    // Wait for all workers to finish
    wg.Wait()
    close(results)

    // Collect results
    for r := range results {
        fmt.Println("Result:", r)
    }
}

func fibonacci(n int) int {
    if n <= 1 {
        return n
    }
    return fibonacci(n-1) + fibonacci(n-2)
}

				
			
  • We use the sync.WaitGroup to synchronize and wait for all workers to finish.
  • Each worker decrements the wait group upon finishing its job.
  • After feeding jobs, we wait for all workers to finish using wg.Wait().

In this chapter, we explored the concepts of worker pools and task queues in Go language. We started from the basics, understanding the purpose and components of worker pools, and then progressed towards implementing a basic worker pool and extending it to include a task queue. Worker pools and task queues are essential concurrency patterns in Go, enabling efficient and scalable concurrent processing of tasks. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India