golang work pool
gowp
golang worker pool , 執行緒池,工作池
- 併發限制 goroutine 池。
- 限制任務執行的併發性,而不是排隊的任務數。
- 無論排隊多少任務,都不會阻止提交任務。
通過佇列支援
golang 工作池公共庫
支援最大任務數,放到工作池裡面 並等待全部完成
package main
import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
)
func main() {
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)
}
return nil
})
}
wp.Wait()
fmt.Println("down")
}
支援錯誤返回
package main
import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
)
func main() {
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")
}
支援判斷是否完成 (非阻塞)
package main
import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
)
func main() {
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的值
time.Sleep(1 * time.Second)
}
return nil
})
fmt.Println(wp.IsDone())
}
wp.Wait()
fmt.Println(wp.IsDone())
fmt.Println("down")
}
支援同步等待結果
package main
import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
)
func main() {
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
})
}
err := wp.Wait()
if err != nil {
fmt.Println(err)
}
fmt.Println("down")
}
相關文章
- Oracle 的 KEEP pool, RECYCLE pool和DEFAULT poolOracle
- work 10月
- Android workAndroid
- golang原始碼分析:sync.Pool 如何從讀寫加鎖到無鎖Golang原始碼
- MySQL-workbench not workMySql
- queue:work 原理分析
- RabbitMQ-Work模式MQ模式
- 【GO】Ready To WorkGo
- Pair_Work ProjectAIProject
- angularjs work with reactAngularJSReact
- ORACLE記憶體管理 之五 SGA variable pool,shared_pool,large_pool,java_poolOracle記憶體Java
- How does a HashMap work in JAVAHashMapJava
- how to fix bug in daily workAI
- react work with angularjs togetherReactAngularJS
- 2 Why Firms Work Together
- Websphere (WAS) Work Load ManagementWeb
- Project Management - 2) Estimate Your WorkProject
- SQL Work Areas(101)SQL
- Application PoolAPP
- Castor JDO and PoolAST
- oracle large poolOracle
- Share Pool理解
- Java Pool (93)Java
- dbms_shared_pool keep物件到share pool中物件
- 【Shared Pool】使用DBMS_SHARED_POOL包將PL/SQL大物件儲存到Shared PoolSQL物件
- (翻譯) How i work - Taylor Otwell
- How Logs Work On MySQL With InnoDB TablesMySql
- RabbitMQ學習筆記-Work QueuesMQ筆記
- 簡介我的 Git Work FlowGit
- How PCTFREE and PCTUSED Work Together(十)
- How does an Electric Motor work? (DC Motor)
- Oracle shared poolOracle
- oracle stream pool sizeOracle
- 分割槽表放入keep pool,recycle pool的問題及解析
- _shared_pool_reserved_pct or shared_pool_reserved_size with ASMMASM
- 簡介我的-Git-Work-FlowGit
- Solaris 5.8 CRON doesn't work with regular user
- WPF Backgroundworker dowork work completed progress changed