GitHub Link: https://github.com/txaty/gool
There are already many good Goroutine pool implementations, and I love their designs so much.
Here I post my own. I made this library several months ago for the parallel execution of the Merkle Tree library.
Although the overhead of Goroutine is significantly lower than a regular thread, process, pooling, and reusing goroutines property can still help improve the program performance (and it did help with the performance of the Merkle Tree library).
The methods of my goroutine pool are just like Python ThreadPoolExecutor:
-
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 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 it, you need to define the following:
- Handler function: handler
func(A) R
, and - Argument:
arg A
.
With types A
and R
being arbitrary generic types.
When creating a new pool, you can specify the number of workers numWorkers and the task queue size cap.
Please feel free to take a look and give some comments. Thanks!