https://github.com/bzsome/chaogo
Golang 工具类:队列,线程池
https://github.com/bzsome/chaogo
Last synced: 27 days ago
JSON representation
Golang 工具类:队列,线程池
- Host: GitHub
- URL: https://github.com/bzsome/chaogo
- Owner: bzsome
- License: mit
- Created: 2020-05-07T02:15:01.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-07T03:50:42.000Z (about 6 years ago)
- Last Synced: 2025-01-05T10:12:16.811Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/xxjwxc/gowp)
[](https://goreportcard.com/report/github.com/bzsome/chaoGo)
[](https://codecov.io/gh/xxjwxc/gowp)
[](https://godoc.org/github.com/bzsome/chaoGo)
[](https://github.com/avelino/awesome-go)
## golang worker pool
### [中文说明](README_cn.md)
- Concurrency limiting goroutine pool.
- Limits the concurrency of task execution, not the number of tasks queued.
- Never blocks submitting tasks, no matter how many tasks are queued.
- Support timeout
- Support through security queues [queue](https://github.com/bzsometest/public/tree/master/myqueue)
## Installation
The simplest way to install the library is to run:
```
$ go get github.com/bzsome/chaoGo
```
### Support the maximum number of tasks, put them in the workpool and wait for them to be completed
## Example
```
package main
import (
"fmt"
"time"
"github.com/bzsome/chaoGo/workpool"
)
func main() {
wp := workpool.New(10) // Set the maximum number of threads
for i := 0; i < 20; i++ { // Open 20 requests
ii := i
wp.Do(func() error {
for j := 0; j < 10; j++ { // 0-10 values per print
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
time.Sleep(1 * time.Second)
}
//time.Sleep(1 * time.Second)
return nil
})
}
wp.Wait()
fmt.Println("down")
}
```
### Support for error return
```
package main
import (
"fmt"
"time"
"github.com/bzsome/chaoGo/workpool"
)
func main() {
wp := workpool.New(10) // Set the maximum number of threads
for i := 0; i < 20; i++ {
ii := i
wp.Do(func() error {
for j := 0; j < 10; j++ { // 0-10 values per print
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
if ii == 1 {
return errors.Cause(errors.New("my test err")) // have err return
}
time.Sleep(1 * time.Second)
}
return nil
})
}
err := wp.Wait()
if err != nil {
fmt.Println(err)
}
fmt.Println("down")
}
```
### Supporting judgement of completion (non-blocking)
```
package main
import (
"fmt"
"time"
"github.com/bzsome/chaoGo/workpool"
)
func main() {
wp := workpool.New(5) // Set the maximum number of threads
for i := 0; i < 10; i++ {
// ii := i
wp.Do(func() error {
for j := 0; j < 5; j++ {
//fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
time.Sleep(1 * time.Second)
}
return nil
})
fmt.Println(wp.IsDone())
}
wp.Wait()
fmt.Println(wp.IsDone())
fmt.Println("down")
}
```
### Support synchronous waiting for results
```
package main
import (
"fmt"
"time"
"github.com/bzsome/chaoGo/workpool"
)
func main() {
wp := workpool.New(5) // Set the maximum number of threads
for i := 0; i < 10; i++ {
ii := i
wp.DoWait(func() error {
for j := 0; j < 5; j++ {
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
// if ii == 1 {
// return errors.New("my test err")
// }
time.Sleep(1 * time.Second)
}
return nil
//time.Sleep(1 * time.Second)
//return errors.New("my test err")
})
}
err := wp.Wait()
if err != nil {
fmt.Println(err)
}
fmt.Println("down")
}
```
### Support timeout exit
```
package main
import (
"fmt"
"time"
"time"
"github.com/bzsome/chaoGo/workpool"
)
func main() {
wp := workpool.New(5) // Set the maximum number of threads
wp.SetTimeout(time.Millisecond) // set max timeout
for i := 0; i < 10; i++ {
ii := i
wp.DoWait(func() error {
for j := 0; j < 5; j++ {
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
time.Sleep(1 * time.Second)
}
return nil
})
}
err := wp.Wait()
if err != nil {
fmt.Println(err)
}
fmt.Println("down")
}
```