workers

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2022 License: MIT Imports: 2 Imported by: 3

README

Tests Go Report Card GoDoc

A simple Go library to set up tasks to be executed in parallel.

package main

import (
	"context"
	"log"

	"github.com/bep/workers"
)

func main() {
	// Max 4 tasks to be executed in parallel.
	w := workers.New(4)
	r, _ := w.Start(context.Background())

	r.Run(func() error {
		return nil
	})

	// ... run more tasks.

	if err := r.Wait(); err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Package workers implements a parallel task executor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runner

type Runner interface {
	Run(func() error)
	Wait() error
}

Runner wraps the lifecycle methods of a new task set.

Run wil block until a worker is available or the context is cancelled, and then run the given func in a new goroutine. Wait will wait for all the running goroutines to finish.

type Workforce added in v1.1.0

type Workforce struct {
	// contains filtered or unexported fields
}

Workforce configures a task executor with the most number of tasks to be executed in parallel.

func New

func New(numWorkers int) *Workforce

New creates a new Workforce with the given number of workers.

func (*Workforce) Start added in v1.1.0

func (w *Workforce) Start(ctx context.Context) (Runner, context.Context)

Start starts a new Runner.

Jump to

Keyboard shortcuts

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