workers

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 1 Imported by: 0

README

workers

simple worker pool

GoDoc Build Status codecov License

The workers package implements a simple fixed-size pool of goroutines for executing functions concurrently. The purpose is to limit concurrency to the number of goroutines in the pool.

When all workers are busy, the channel for submitting tasks blocks. If you are looking for a worker pool that never blocks when submitting tasks, see workerpool.

Installation

$ go get github.com/gammazero/workers

Example

package main

import (
	"fmt"
	"github.com/gammazero/workers"
)

func main() {
	do, done := workers.New(5)
	requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}

	for _, r := range requests {
		do <- func() {
			fmt.Println("Handling request:", r)
		}
	}
	close(do) // stop workers
	<-done    // wait for all workers to exit

	fmt.Println("All done")
}

Documentation

Overview

Package workers implements a simple fixed-size pool of goroutines for executing functions concurrently. The purpose is to limit the amount of concurrency to the number of goroutines in the pool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(numWorkers int) (chan<- func(), <-chan struct{})

New creates a pool of goroutines that run the functions sent to them over the write channel. New returns two channels, a write-only "work" channel and a read-only "done" channel.

The first channel is a write-only channel for sending task functions for the workers to run. When all workers are busy, the channel blocks. Close this channel To stop the workers.

The second channel is a read-only channel that is closed when all workers have exited.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL