2021-02-01 20:15:05 +05:00
|
|
|
# gwp
|
2021-02-01 20:50:40 +05:00
|
|
|
|
|
|
|
Simple goroutine worker pool written in Go.
|
|
|
|
|
|
|
|
Status: Work in progress.
|
2021-02-03 00:31:01 +05:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nxshock/gwp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Job function
|
|
|
|
func f(i int) error {
|
|
|
|
log.Printf("Job №%d", i)
|
|
|
|
|
|
|
|
// Simulate work
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-02-03 18:54:32 +05:00
|
|
|
|
|
|
|
worker := gwp.New(4) // Create pool with specified number of workers
|
2021-03-27 10:52:23 +05:00
|
|
|
worker.ShowProgress = true // Enable progress indicator
|
|
|
|
worker.EstimateCount = 100 // Set total number on jobs to calculate ETA
|
2021-02-03 00:31:01 +05:00
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
n := i
|
|
|
|
// Send job
|
|
|
|
worker.Add(func() error {
|
|
|
|
return f(n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait all jobs to complete
|
|
|
|
worker.CloseAndWait()
|
|
|
|
}
|
|
|
|
```
|