gool

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: MIT Imports: 1 Imported by: 1

README

Gool

Go Reference Go Report Card codecov

A generic goroutine pool just like Python ThreadPoolExecutor.

Gool provides the following methods:

  • Submit: Submit a task and return the result (if any).
  • AsyncSubmit: Submit a task and return a future of the result (if any), the future is actually the result channel.
  • Map: Submit a bundle of tasks and return the results in order (if any).
  • AsyncMap: Submit a bundle of tasks and return the futures of the results (if any), the futures are the result channels.

To use Gool, you need to define:

  • Handler function: handler func(A) R, and
  • Argument: arg A

With types A and R being arbitrary types.

You can also specify the number of workers numWorkers and the task queue size cap when creating a new pool.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool[A, R any] struct {
	// contains filtered or unexported fields
}

Pool implements a simple goroutine pool.

func NewPool

func NewPool[A, R any](numWorkers, cap int) *Pool[A, R]

NewPool creates a new goroutine pool with the given number of workers and job queue capacity. If numWorkers is less than 1, it will be set to the number of CPUs. If cap (task queue capacity) is less than 1, it will be set to twice the number of workers.

func (*Pool[A, R]) AsyncMap

func (p *Pool[A, R]) AsyncMap(handler func(A) R, args []A) []chan R

AsyncMap submits a batch of tasks and returns the channel to wait for the results.

func (*Pool[A, R]) AsyncSubmit

func (p *Pool[A, R]) AsyncSubmit(handler func(A) R, args A) chan R

AsyncSubmit submits a task and returns the channel to wait for the result.

func (*Pool[A, R]) Close

func (p *Pool[A, R]) Close()

Close closes the pool and waits for all the workers to stop.

func (*Pool[A, R]) Map

func (p *Pool[A, R]) Map(handler func(A) R, args []A) []R

Map submits a batch of tasks and waits for the results.

func (*Pool[A, R]) Submit

func (p *Pool[A, R]) Submit(handler func(A) R, args A) R

Submit submits a task and waits for the result.

Jump to

Keyboard shortcuts

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