https://github.com/radutanasa/gothpool
A simple fire-and-forget golang executor pool.
https://github.com/radutanasa/gothpool
executor golang-library goroutine-pool
Last synced: 2 months ago
JSON representation
A simple fire-and-forget golang executor pool.
- Host: GitHub
- URL: https://github.com/radutanasa/gothpool
- Owner: radutanasa
- License: mit
- Created: 2020-04-13T06:44:01.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-30T07:08:46.000Z (over 5 years ago)
- Last Synced: 2024-06-20T13:33:04.296Z (almost 2 years ago)
- Topics: executor, golang-library, goroutine-pool
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Gothpool
Gothpool is a Golang library for running fire-and-forget jobs using a fixed size goroutine pool and processing queue.
## Install
``` sh
go get github.com/radutanasa/gothpool
```
Or, using dep:
``` sh
dep ensure -add github.com/radutanasa/gothpool
```
## Use
Simply instantiate the gothpool (executor pool) with your desired level of parallelism and queue size
and start sending it functions.
``` go
package main
import (
"github.com/radutanasa/gothpool"
)
func main() {
exec := gothpool.New(4, 1e5)
exec.Start()
defer exec.Stop()
exec.Run(func() {
// perform operations
})
}
```
It's important to note that the executor pool will finish its current job queue, but won't receive any new jobs.
A `Run()` call on a stopped pool will return an `ExecPoolStoppedErr` error.
The pool can be restarted by calling the `Start()` function.
**IMPORTANT:** Variables sent to the executor should not change!
This code results in an unexpected behavior, as `i` references different values:
``` go
for i:=0; i<10; i++ {
exec.Run(func() {
println(i)
})
}
```
This results in the correct behavior:
``` go
for i:=0; i<10; i++ {
var j = i
exec.Run(func() {
println(j)
})
}
```