Skip to content

workerpool for asynchronous job executions and handling results from them

License

Notifications You must be signed in to change notification settings

gudgl/workerpool

Repository files navigation

workerpool


made-with-Go GitHub release Go Reference GitHub license

Workerpool is a library for asynchronous execution of jobs and handling results from them. It was made for learning purposes and included generics to touch this topic for the first time. Even that this is not the best place to use it. Try it out you can run concurrent jobs and handle their results concurrently. Open an issue and suggest some improvements so I can learn and maybe this will be usefull to someone.

Install

go get github.com/gudgl/workerpool

Usage

Types

// JobHandler represents something that should be done by the worker
type JobHandler[T any] func() T

// RespHandler represents how to handler workers response to a job
type RespHandler[T any] func(T)

// ConfigOpts represents optional parameters for Config
type ConfigOpts[T any] func(*Config[T])

To start create a new Client

client, ctx := jw.New(
    // send context 
    ctx,
    // send options if needed
)

Options

// WithNumOfWorkers is an option to specify the number of concurrent workers
// the defautl is one
func WithNumOfWorkers[T any](num int) ConfigOpts[T]
// WithNumOfCollectors is an option to specify the number of concurrent collectors
// the defautl is one
func WithNumOfCollectors[T any](num int) ConfigOpts[T]
// WithCollectors is an option to specify the number of concurrent collectors
// the defautl is one
func WithCollectors[T any](collector RespHandler[T]) ConfigOpts[T]

Next run the Go function from the client to start the workers and the collectors

client.Go()

Then publish the jobs

for _, job := range jobs{
    client.PublishJob(job JobHandler)
}

or publish them all at once

client.PublishJobs(jobs)

Last just wait for workers and collectors to finish

client.Wait()

That's all you need to do to get it up running

Collaboration

If you would like to contribute, head on to the issues page for tasks that need help.

Licence

MIT

About

workerpool for asynchronous job executions and handling results from them

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages