forked from xxjwxc/gowp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
谢小军
authored and
谢小军
committed
Sep 15, 2019
1 parent
f44adb4
commit fa12ea9
Showing
7 changed files
with
198 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,102 @@ | ||
# gowp | ||
golang worker pool ,线程池 , 工作池 | ||
## golang worker pool ,线程池 , 工作池 | ||
|
||
- 并发限制goroutine池。 | ||
- 限制任务执行的并发性,而不是排队的任务数。 | ||
- 无论排队多少任务,都不会阻止提交任务。 | ||
- 通过队列支持 | ||
|
||
- golang 工作池公共库 | ||
|
||
### 支持最大任务数, 放到工作池里面 并等待全部完成 | ||
``` | ||
wp := workerpool.New(10) //设置最大线程数 | ||
for i := 0; i < 20; i++ { //开启20个请求 | ||
ii := i | ||
wp.Do(func() error { | ||
for j := 0; j < 10; j++ { //每次打印0-10的值 | ||
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") | ||
``` | ||
|
||
### 支持错误返回 | ||
``` | ||
wp := workerpool.New(10) //设置最大线程数 | ||
for i := 0; i < 20; i++ { //开启20个请求 | ||
ii := i | ||
wp.Do(func() error { | ||
for j := 0; j < 10; j++ { //每次打印0-10的值 | ||
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j)) | ||
if ii == 1 { | ||
return errors.Cause(errors.New("my test err")) //有err 立即返回 | ||
} | ||
time.Sleep(1 * time.Second) | ||
} | ||
return nil | ||
}) | ||
} | ||
err := wp.Wait() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println("down") | ||
``` | ||
|
||
### 支持判断是否完成 (非阻塞) | ||
|
||
``` | ||
wp := workerpool.New(5) //设置最大线程数 | ||
for i := 0; i < 10; i++ { //开启20个请求 | ||
// ii := i | ||
wp.Do(func() error { | ||
for j := 0; j < 5; j++ { //每次打印0-10的值 | ||
//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") | ||
``` | ||
|
||
### 支持同步等待结果 | ||
|
||
``` | ||
wp := workerpool.New(5) //设置最大线程数 | ||
for i := 0; i < 10; i++ { //开启20个请求 | ||
ii := i | ||
wp.DoWait(func() error { | ||
for j := 0; j < 5; j++ { //每次打印0-10的值 | ||
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") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
=========================2019-09-15 20:47:01 ========================= | ||
my test err | ||
gowp/workerpool.TestWorkerPoolError.func1 | ||
/Users/xxj/work/workspace/github/xxjwxc/gowp/workerpool/workerpool_test.go:37 | ||
gowp/workerpool.(*WorkerPool).loop.func1 | ||
/Users/xxj/work/workspace/github/xxjwxc/gowp/workerpool/workerpool.go:133 | ||
runtime.goexit | ||
/usr/local/go/src/runtime/asm_amd64.s:1357 | ||
goroutine 51 [running]: | ||
runtime/debug.Stack(0x0, 0x0, 0x0) | ||
/usr/local/go/src/runtime/debug/stack.go:24 +0xa1 | ||
github.com/xxjwxc/public/mylog.SaveError(0xc0001883c0, 0x133, 0x11b92cb, 0x3) | ||
/Users/xxj/work/path/pkg/mod/github.com/xxjwxc/[email protected]/mylog/mylog.go:100 +0x498 | ||
github.com/xxjwxc/public/mylog.Error(0x11d7480, 0xc000172020) | ||
/Users/xxj/work/path/pkg/mod/github.com/xxjwxc/[email protected]/mylog/mylog.go:62 +0x3d5 | ||
gowp/workerpool.(*WorkerPool).loop.func1(0xc0000da780) | ||
/Users/xxj/work/workspace/github/xxjwxc/gowp/workerpool/workerpool.go:139 +0x34f | ||
created by gowp/workerpool.(*WorkerPool).loop | ||
/Users/xxj/work/workspace/github/xxjwxc/gowp/workerpool/workerpool.go:108 +0xa1 | ||
|
||
=========================end========================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters