duzy / worker

Easier Go Concurrency Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

worker

Package worker implements an easy to use concurrency framework for multiple-job Go program.

GoDoc

Here is a quick example demonstrating the usage.

package example

import "github.com/duzy/worker"

type StepOne struct {
        Param string
}
func (job *StepOne) Go() worker.Result {
        // do job for step one
        return &StepTwo{}
}

type StepTwo struct {
}
func (job *StepTwo) Go() worker.Result {
        // do job for step two
        return &StepThree{}
}

type StepThree struct {
}
func (job *StepThree) Go() worker.Result {
        // do job for step three
        return nil
}

const NumberOfConcurrency = 10

func main() {
        w := worker.SpawnN(NumberOfConcurrency)
        w.Do(&StepOne{ "anything goes" })
        w.Do(&StepOne{ "anything goes" })
        w.Do(&StepOne{ "anything goes" })
        w.Do(&StepOne{ "anything goes" })
        w.Kill()

        w = worker.SpawnN(3)
        sentry := w.Sentry()
        sentry.Guard(&StepOne{ "anything goes" })
        sentry.Guard(&StepOne{ "anything goes" })
        sentry.Guard(&StepOne{ "anything goes" })
        for result, _ := range sentry.Wait() {
            // ...
        }
}

About

Easier Go Concurrency Framework

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Go 100.0%