Giter Site home page Giter Site logo

sbp-bvanb / workers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wimspaargaren/workers

0.0 0.0 0.0 21 KB

A generic worker pool, providing type inference based on the specified processing function.

License: MIT License

Go 93.91% Makefile 6.09%

workers's Introduction

Go Generic Worker Pool

Build Status Go Reference

This repository provides a generic, easy to use worker pool.

Initialise a buffered or unbuffered pool with a processing function defined as func(U) (T, error). The result types will be inferred from the compiler, from the provided processing function.

The amount of workers and buffer size can be configured.

In case the provided context gets canceled, the workers will stop processing and jobs can no longer be added to the pool. The AwaitResults function will return an error ErrContextCanceled.

Example usage

package main

import (
	"context"
	"fmt"
	"math/rand"
	"time"

	"github.com/wimspaargaren/workers"
)

// an example processor
type myRandomProcessor struct {
	rand *rand.Rand
}

func (p *myRandomProcessor) process(i int) (string, error) {
	duration := time.Millisecond * time.Duration(p.rand.Intn(100))
	time.Sleep(duration)
	return fmt.Sprintf("processed %d in %d milliseconds", i, duration.Milliseconds()), nil
}

func main() {
	randomProcessor := myRandomProcessor{
		rand: rand.New(rand.NewSource(time.Now().Unix())),
	}
    	// create a new default buffered pool
	workerPool := workers.NewBufferedPool(context.Background(),
		randomProcessor.process,
	)
	go func() {
        	// add jobs to process
		for i := 0; i < 42; i++ {
			err := workerPool.AddJob(i)
			if err != nil {
				// handle me
			}
		}
		// call done, to indicate all jobs have been added to the pool
		workerPool.Done()
	}()

    	// await until all jobs are processed
	results, err := workerPool.AwaitResults()
	if err != nil {
		// handle me
	}
	for i, r := range results {
		fmt.Println("i", i, "r", r)
	}
}

workers's People

Contributors

wimspaargaren avatar dependabot[bot] 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.