V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
xxjwxc
V2EX  ›  Go 编程语言

golang worker pool , 工作池,线程池

  •  
  •   xxjwxc ·
    xxjwxc · 2020-01-02 17:30:48 +08:00 · 3293 次点击
    这是一个创建于 1576 天前的主题,其中的信息可能已经有所发展或是发生改变。

    gowp

    golang worker pool ,线程池 , 工作池

    • 并发限制 goroutine 池。

    • 限制任务执行的并发性,而不是排队的任务数。

    • 无论排队多少任务,都不会阻止提交任务。

    • 通过队列支持

    • golang 工作池公共库

    支持最大任务数, 放到工作池里面 并等待全部完成

    package main
    
    import (
    	"fmt"
    	"time"
    
    	"github.com/xxjwxc/gowp/workpool"
    )
    
    func main() {
    	wp := workpool.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")
    }
    

    支持错误返回

    package main
    
    import (
    	"fmt"
    	"time"
    
    	"github.com/xxjwxc/gowp/workpool"
    )
    
    func main() {
    	wp := workpool.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/workpool"
    )
    
    func main() {
    	wp := workpool.New(5)              //设置最大线程数
    	for i := 0; i < 10; i++ { //开启 20 个请求
    		//	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")
    }
    

    支持同步等待结果

    package main
    
    import (
    	"fmt"
    	"time"
    
    	"github.com/xxjwxc/gowp/workpool"
    )
    
    func main() {
    	wp := workpool.New(5)              //设置最大线程数
    	for i := 0; i < 10; i++ { //开启 20 个请求
    		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")
    }
    

    代码地址:gowp 喜欢请给星

    6 条回复    2020-01-03 10:09:20 +08:00
    mcfog
        1
    mcfog  
       2020-01-02 19:28:35 +08:00
    xxjwxc
        2
    xxjwxc  
    OP
       2020-01-02 19:54:29 +08:00
    @mcfog 功能比他更丰富,而且,技术更新。用的 sync.WaitGroup
    whoami9894
        3
    whoami9894  
       2020-01-02 20:13:44 +08:00
    @xie1xiao1jun
    不是我杠啊,select+channel 和 wg 之间还有啥新旧技术之分吗
    xxjwxc
        4
    xxjwxc  
    OP
       2020-01-02 20:38:36 +08:00
    @whoami9894 select+channel 模式是会阻塞的(超过最大缓冲数)。
    mengzhuo
        5
    mengzhuo  
       2020-01-02 22:14:26 +08:00
    @xie1xiao1jun 然后放个链表里不就好了……
    xxjwxc
        6
    xxjwxc  
    OP
       2020-01-03 10:09:20 +08:00
    @mengzhuo 是的,实现非阻塞方式很多种。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1095 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:22 · PVG 07:22 · LAX 16:22 · JFK 19:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.