├── .gitignore ├── LICENSE ├── go.mod ├── parallel.go ├── readme.md ├── tests ├── parallel_test.go └── workers_test.go └── workers.go /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | log -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 桥边红药 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/qbhy/parallel 2 | 3 | go 1.15 4 | -------------------------------------------------------------------------------- /parallel.go: -------------------------------------------------------------------------------- 1 | package parallel 2 | 3 | import ( 4 | "errors" 5 | "sync" 6 | ) 7 | 8 | type Parallel struct { 9 | Callbacks []func() interface{} 10 | channel chan int 11 | status ParallelStatus 12 | } 13 | 14 | type ParallelStatus int 15 | 16 | const ( 17 | NORMAL = iota 18 | LISTENING 19 | STOPPED 20 | GRACEFUL_STOP 21 | ) 22 | 23 | var ( 24 | StoppedError = errors.New("the process has stopped") 25 | ) 26 | 27 | func NewParallel(concurrent int) *Parallel { 28 | return &Parallel{ 29 | Callbacks: make([]func() interface{}, 0), 30 | channel: make(chan int, concurrent), 31 | status: NORMAL, 32 | } 33 | } 34 | 35 | func (this *Parallel) Add(callback func() interface{}) error { 36 | if this.IsStopped() { 37 | return StoppedError 38 | } else { 39 | this.Callbacks = append(this.Callbacks, callback) 40 | return nil 41 | } 42 | } 43 | 44 | func (this *Parallel) IsStopped() bool { 45 | return this.status == STOPPED || this.status == GRACEFUL_STOP 46 | } 47 | 48 | func (this *Parallel) Wait() (results map[int]interface{}) { 49 | queues := this.Callbacks 50 | this.Clear() 51 | 52 | wg := sync.WaitGroup{} 53 | wg.Add(len(queues)) 54 | resultMutex := sync.RWMutex{} 55 | 56 | results = map[int]interface{}{} 57 | for key, callback := range queues { 58 | this.channel <- 0 59 | go func(key int, callback func() interface{}) { 60 | // 捕捉异常 61 | defer func() { 62 | if err := recover(); err != nil { 63 | resultMutex.Lock() 64 | results[key] = err 65 | resultMutex.Unlock() 66 | } 67 | 68 | <-this.channel 69 | wg.Done() 70 | }() 71 | 72 | result := callback() 73 | 74 | resultMutex.Lock() 75 | results[key] = result 76 | resultMutex.Unlock() 77 | }(key, callback) 78 | } 79 | 80 | wg.Wait() 81 | 82 | return 83 | } 84 | 85 | func (this *Parallel) Run() map[int]interface{} { 86 | return this.Wait() 87 | } 88 | 89 | func (this *Parallel) Stop() { 90 | this.status = STOPPED 91 | } 92 | 93 | func (this *Parallel) GracefulStop() { 94 | this.status = GRACEFUL_STOP 95 | } 96 | 97 | func (this *Parallel) Listen() (err error) { 98 | this.status = LISTENING 99 | 100 | defer func() { 101 | if this.status == GRACEFUL_STOP { 102 | this.Wait() 103 | } 104 | }() 105 | 106 | for { 107 | if this.status == LISTENING { 108 | this.Wait() 109 | } else { 110 | break 111 | } 112 | } 113 | 114 | return err 115 | } 116 | 117 | func (this *Parallel) Clear() { 118 | this.Callbacks = make([]func() interface{}, 0) 119 | } 120 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Parallel 2 | golang 协程并行库,可以指定并发数量,可以当做简单的队列使用 3 | 4 | ## 安装 - installation 5 | ```bash 6 | go get github.com/qbhy/parallel 7 | ``` 8 | 9 | ## 使用 - usage 10 | 1. 普通用法 11 | ```go 12 | package tests 13 | 14 | import ( 15 | "errors" 16 | "fmt" 17 | "github.com/qbhy/parallel" 18 | "testing" 19 | ) 20 | 21 | func TestParallel(t *testing.T) { 22 | // 最多 10 个协程同时运行 23 | p := parallel.NewParallel(10) 24 | 25 | p.Add(func() interface{} { 26 | return "执行了" 27 | }) 28 | 29 | p.Add(func() interface{} { 30 | panic(errors.New("报错了")) 31 | }) 32 | 33 | fmt.Println(p.Wait()) 34 | //会输出 map[0:执行了 1:报错了] 35 | } 36 | ``` 37 | 2. 简单的队列 38 | ```go 39 | package tests 40 | 41 | import ( 42 | "fmt" 43 | "github.com/qbhy/parallel" 44 | "testing" 45 | "time" 46 | ) 47 | 48 | // 测试优雅退出 49 | func TestParallelGracefulStop(t *testing.T) { 50 | p := parallel.NewParallel(2) 51 | 52 | go func() { 53 | i := 0 54 | for ; i < 10; i++ { 55 | (func(i int) { 56 | if i >= 5 { 57 | p.GracefulStop() 58 | } 59 | result := p.Add(func() interface{} { 60 | time.Sleep(time.Second) 61 | fmt.Printf("每隔1秒执行一次 %d \n", i) 62 | return nil 63 | }) 64 | 65 | fmt.Printf("添加结果: %v, i: %d\n", result, i) 66 | })(i) 67 | } 68 | }() 69 | 70 | fmt.Println(p.Listen()) 71 | } 72 | ``` 73 | > 也可以参考 `tests/parallel_test.go` 的代码 74 | 75 | https://github.com/qbhy/parallel 76 | qbhy0715@qq.com 77 | -------------------------------------------------------------------------------- /tests/parallel_test.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "github.com/qbhy/parallel" 7 | "testing" 8 | "time" 9 | ) 10 | 11 | func TestParallel(t *testing.T) { 12 | p := parallel.NewParallel(2) 13 | 14 | p.Add(func() interface{} { 15 | time.Sleep(time.Second * 2) 16 | return "执行了" 17 | }) 18 | p.Add(func() interface{} { 19 | time.Sleep(time.Second * 2) 20 | return "执行了" 21 | }) 22 | p.Add(func() interface{} { 23 | panic(errors.New("报错了")) 24 | }) 25 | p.Add(func() interface{} { 26 | time.Sleep(time.Second * 5) 27 | return "执行了" 28 | }) 29 | 30 | fmt.Println(p.Wait()) 31 | } 32 | 33 | // 监听模式 34 | func TestParallelListen(t *testing.T) { 35 | p := parallel.NewParallel(2) 36 | 37 | go func() { 38 | i := 0 39 | for ; i < 10; i++ { 40 | (func(i int) { 41 | result := p.Add(func() interface{} { 42 | time.Sleep(time.Second) 43 | fmt.Printf("每隔1秒执行一次 %d \n", i) 44 | return nil 45 | }) 46 | 47 | fmt.Printf("添加结果: %v, i: %d \n", result, i) 48 | })(i) 49 | } 50 | 51 | ticker := time.NewTicker(time.Second) 52 | 53 | for { 54 | <-ticker.C 55 | i++ 56 | (func(i int) { 57 | p.Add(func() interface{} { 58 | time.Sleep(time.Second) 59 | fmt.Println("每隔1秒执行一次", i) 60 | return nil 61 | }) 62 | })(i) 63 | 64 | if i > 20 { 65 | p.Stop() 66 | break 67 | } 68 | } 69 | }() 70 | 71 | fmt.Println(p.Listen()) 72 | } 73 | 74 | // 测试优雅退出 75 | func TestParallelGracefulStop(t *testing.T) { 76 | p := parallel.NewParallel(2) 77 | 78 | go func() { 79 | i := 0 80 | for ; i < 10; i++ { 81 | (func(i int) { 82 | if i >= 5 { 83 | p.GracefulStop() 84 | } 85 | result := p.Add(func() interface{} { 86 | time.Sleep(time.Second) 87 | fmt.Printf("每隔1秒执行一次 %d \n", i) 88 | return nil 89 | }) 90 | 91 | fmt.Printf("添加结果: %v, i: %d\n", result, i) 92 | })(i) 93 | } 94 | }() 95 | 96 | fmt.Println(p.Listen()) 97 | } 98 | -------------------------------------------------------------------------------- /tests/workers_test.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "fmt" 5 | "github.com/qbhy/parallel" 6 | "testing" 7 | "time" 8 | ) 9 | 10 | func TestWorkers(t *testing.T) { 11 | workers := parallel.NewWorkers(3) 12 | go func() { 13 | for i := 0; i < 100; i++ { 14 | (func(i int) { 15 | _ = workers.Handle(func() { 16 | time.Sleep(1 * time.Second) 17 | fmt.Println("worked", i) 18 | }) 19 | })(i) 20 | } 21 | }() 22 | time.Sleep(3 * time.Second) 23 | workers.Stop() 24 | } 25 | -------------------------------------------------------------------------------- /workers.go: -------------------------------------------------------------------------------- 1 | package parallel 2 | 3 | import ( 4 | "errors" 5 | "log" 6 | ) 7 | 8 | type Workers struct { 9 | processes int 10 | pipe chan bool 11 | stopped bool 12 | } 13 | 14 | func NewWorkers(num int) *Workers { 15 | return &Workers{ 16 | processes: num, 17 | pipe: make(chan bool, num), 18 | } 19 | } 20 | 21 | var ( 22 | WorkersStoppedError = errors.New("the process has stopped") 23 | ) 24 | 25 | func (workers *Workers) Handle(job func()) error { 26 | if workers.stopped { 27 | return WorkersStoppedError 28 | } 29 | workers.pipe <- true 30 | 31 | go func() { 32 | defer func() { 33 | <-workers.pipe 34 | if panicValue := recover(); panicValue != nil { 35 | log.Default().Println(panicValue) 36 | } 37 | }() 38 | job() 39 | }() 40 | 41 | return nil 42 | } 43 | 44 | func (workers *Workers) Stop() { 45 | workers.stopped = true 46 | } 47 | --------------------------------------------------------------------------------