Share a very easy-to-use GO concurrency control library!

藏色散人
Release: 2022-12-21 18:01:40
forward
4661 people have browsed it

This article will introduce you to the relevant knowledge about Golang and talk about a very easy-to-use golang concurrency control library. I hope it will be helpful to everyone.

Share a very easy-to-use GO concurrency control library!

Concurrency

Build Status

##install

GOPROXY=https://goproxy.cn go get -v github.com/lxzan/concurrency@latest
Copy after login

Feature

    Limit on the number of concurrent coroutines
  • Support
  • contex.Contex
  • Support
  • panic recover, return contains The error
  • recursively implements task scheduling of the error stack and does not rely on
  • time.Ticker and channel

Usage

    WorkerGroup work group, adding a group of tasks and waiting for execution to complete, can be a good alternative to
  • WaitGroup.
package mainimport (
    "fmt"
    "github.com/lxzan/concurrency"
    "sync/atomic")func main() {
    sum := int64(0)
    w := concurrency.NewWorkerGroup()
    for i := int64(1); i <= 10; i++ {
        w.AddJob(concurrency.Job{
            Args: i,
            Do: func(args interface{}) error {
                fmt.Printf("%v ", args)
                atomic.AddInt64(&sum, args.(int64))
                return nil
            },
        })
    }
    w.StartAndWait()
    fmt.Printf("sum=%d\n", sum)}
Copy after login
4 5 6 7 8 9 10 1 3 2 sum=55
Copy after login
    WorkerQueue Work queue, you can continuously add tasks to it, and execute it once the CPU resources are free [Recommendation:
  • go tutorial]
package mainimport (
    "fmt"
    "github.com/lxzan/concurrency"
    "time")func Add(args interface{}) error {
    arr := args.([]int)
    ans := 0
    for _, item := range arr {
        ans += item    }
    fmt.Printf("args=%v, ans=%d\n", args, ans)
    return nil}func Mul(args interface{}) error {
    arr := args.([]int)
    ans := 1
    for _, item := range arr {
        ans *= item    }
    fmt.Printf("args=%v, ans=%d\n", args, ans)
    return nil}func main() {
    args1 := []int{1, 3}
    args2 := []int{1, 3, 5}
    w := concurrency.NewWorkerQueue()
    w.AddJob(
        concurrency.Job{Args: args1, Do: Add},
        concurrency.Job{Args: args1, Do: Mul},
        concurrency.Job{Args: args2, Do: Add},
        concurrency.Job{Args: args2, Do: Mul},
    )
    w.StopAndWait(30*time.Second)}
Copy after login
args=[1 3], ans=4args=[1 3 5], ans=15args=[1 3], ans=3args=[1 3 5], ans=9
Copy after login

The above is the detailed content of Share a very easy-to-use GO concurrency control library!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!