Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qbhy/parallel
https://github.com/qbhy/parallel
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/qbhy/parallel
- Owner: qbhy
- License: mit
- Created: 2021-08-17T09:05:14.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-02-06T02:34:25.000Z (almost 3 years ago)
- Last Synced: 2024-06-19T17:50:20.780Z (7 months ago)
- Language: Go
- Size: 9.77 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Parallel
golang 协程并行库,可以指定并发数量,可以当做简单的队列使用## 安装 - installation
```bash
go get github.com/qbhy/parallel
```## 使用 - usage
1. 普通用法
```go
package tests
import (
"errors"
"fmt"
"github.com/qbhy/parallel"
"testing"
)
func TestParallel(t *testing.T) {
// 最多 10 个协程同时运行
p := parallel.NewParallel(10)
p.Add(func() interface{} {
return "执行了"
})
p.Add(func() interface{} {
panic(errors.New("报错了"))
})
fmt.Println(p.Wait())
//会输出 map[0:执行了 1:报错了]
}
```
2. 简单的队列
```go
package tests
import (
"fmt"
"github.com/qbhy/parallel"
"testing"
"time"
)
// 测试优雅退出
func TestParallelGracefulStop(t *testing.T) {
p := parallel.NewParallel(2)
go func() {
i := 0
for ; i < 10; i++ {
(func(i int) {
if i >= 5 {
p.GracefulStop()
}
result := p.Add(func() interface{} {
time.Sleep(time.Second)
fmt.Printf("每隔1秒执行一次 %d \n", i)
return nil
})
fmt.Printf("添加结果: %v, i: %d\n", result, i)
})(i)
}
}()
fmt.Println(p.Listen())
}
```
> 也可以参考 `tests/parallel_test.go` 的代码https://github.com/qbhy/parallel
[email protected]