workerpool

package module
v0.0.0-...-3de3657 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2018 License: MIT Imports: 3 Imported by: 0

README

Worker Pool

A flexible Go worker pool model library using reflect.

Just toss all your jobs at it and it manages the concurrency using a pool of workers.

Look and Feel

// the function that does the job
func Print(num int) {
	time.Sleep(time.Duration(6-num) * time.Second)
	fmt.Println(num)
}

// fn and concurrency
wp, err := New(Print, 10)
if err != nil {
	panic("can't create workerqueue " + err.Error())
}
for i := 0; i <= 5; i++ {
	// send a job
	wp.Work(i)
}
// close the job queue and start waiting all the job to be finished
// you can ignore this if don't have to wait
wp.Wait()

For more examples, see main_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

func New

func New(fn interface{}, concurrency int) (wp *Pool, err error)

Create a new worker pool.

func (*Pool) Close

func (wp *Pool) Close()

Close the job queue channel.

func (*Pool) Wait

func (wp *Pool) Wait()

Close the job queue channel and wait for all the jobs to be done.

func (*Pool) Work

func (wp *Pool) Work(args ...interface{})

Add a job to the job queue for processing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL