GithubHelp home page GithubHelp logo

go-worker-pool's Introduction

A Go Worker Pool Implementation

Maintainability Test Coverage

Install

go get github.com/ebarti/go-worker-pool

Usage:

Simple usage

Instantiate new worker pools via the NewWorkerPool factory method.

type MyTask struct {
	c int32
}

func NewTask() *MyTask {
	return &MyTask{}
}

// Add a Run function with the appropriate signature to implement the Task interface 
func (me *MyTask) Run(wg WorkerPool, in interface{}) error {
	atomic.AddInt32(me.c, 1)
}

func main() {
	myTask := NewTask()
    workerPool := NewWorkerPool(context.Background(), myTask, 10).CancelOnSignal(os.Interrupt).Work()
    
    for i := 0; i < 10; i++ {
        workerpool.Send(i)
    }
    
    // Close the workerpool to wait for its completion
    if err := workerPool.Close(); err != nil {
        fmt.Println(err)
        return
    }
    
    fmt.Println()
}

Complex Usage

type type1 string
type type2 string

type MyTask0 struct {}

type MyTask1 struct {
	store []type1
	mu *sync.RWMutex
}

type MyTask2 struct {
	store []type2
	mu *sync.RWMutex
}

func NewTask0() *MyTask0 {
	return &MyTask0{}
}

func NewTask1() *MyTask1 {
	return &MyTask1{mu: new(sync.RWMutex)}
}

func NewTask2() *MyTask2 {
	return &MyTask2{mu: new(sync.RWMutex)}
}

func (my *MyTask0) Run(wg WorkerPool, in interface{}) error {
    i, ok := in.(int)
    if !ok {
        return errors.New("MyTask0 invalid input type")
    }
    outtype1 := type1(strconv.Itoa(i) + " type1")
    outtype2 := type2(strconv.Itoa(i) + " type2")
    wg.Out(outtype1)
    wg.Out(outtype2)
    return nil
}

func (my *MyTask1) Run(wg WorkerPool, in interface{}) error {
    i, ok := in.(type1)
    if !ok {
        return errors.New("MyTask1 invalid input type")
    }
    my.mu.Lock()
    my.store = append(my.store, i)
    my.mu.Unlock()
    return nil
}


func (my *MyTask2) Run(wg WorkerPool, in interface{}) error {
    i, ok := in.(type2)
    if !ok {
        return errors.New("MyTask2 invalid input type")
    }

    my.mu.Lock()
    my.store = append(my.store, i)
    my.mu.Unlock()
    return nil
}

func main() {
    ctx := context.Background()
    t0 := NewTask0()
    t1 := NewTask1()
    t2 := NewTask2()
    worker0 := NewWorkerPool(ctx, NewTask0(), 20).Work()
    worker1 := NewWorkerPool(ctx, t1, 20).ReceiveFromWithType(reflect.TypeOf(t1), worker0).Work()
    worker2 := NewWorkerPool(ctx, t2, 20).ReceiveFromWithType(reflect.TypeOf(t1), worker0).Work()
    for i := 0; i < 2000; i++ {
        workerOne.Send(i)
    }
    if err := worker0.Close(); err != nil {
        fmt.Println(err)
    }
    if err := worker1.Close(); err != nil {
        fmt.Println(err)
    }
    if err := worker2.Close(); err != nil {
        fmt.Println(err)
    }
}

Version log

  • v0.1.0 Initial release

go-worker-pool's People

Contributors

ebarti avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.