golang并发控制

给大家推荐一个并发控制包, 有了github.com/lxzan/runner, 妈妈再也不担心我的爬虫把别人家网站搞挂了

  • package
github.com/lxzan/runner
  • example
// 并发10个任务, 每10ms检查一次新任务
package main

import (
    "context"
    "fmt"
    "github.com/lxzan/runner"
    "os"
    "os/signal"
    "syscall"
    "time"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    r := runner.New(ctx, 10, 10*time.Millisecond)
    r.OnClose = func(data []runner.TaskInterface) {
        println(fmt.Sprintf("rest tasks: %d", len(data)))
    }

    for i := 0; i < 100; i++ {
        tmp := i
        r.Add(&runner.Task{Work: func() error {
            println(tmp)
            time.Sleep(500 * time.Millisecond)
            return nil
        }})
    }

    quit := make(chan os.Signal)
    signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
    <-quit
    cancel()
    <-r.Wait
}

如果觉得好用的话, 给我一个star吧 :)


codebeast
51 声望0 粉丝