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.
go get github.com/gudgl/workerpool
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
If you would like to contribute, head on to the issues page for tasks that need help.