gorpool

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 3 Imported by: 2

README

gorpool

Simple Goroutine pool

How to use

go get github.com/yale8848/gorpool

Simple example


package main

import (
	"github.com/yale8848/gorpool"
	"time"
	"fmt"
)

func main() {

    // workerNum is worker number of goroutine pool ,one worker have one goroutine ,
    // jobNum is job number of job pool
	p := gorpool.NewPool(5, 10).
		Start()
	defer p.StopAll()
	for i := 0; i < 100; i++ {
		count := i
		p.AddJob(func() {
			time.Sleep(10 * time.Millisecond)
			fmt.Printf("%d\r\n", count)
		})

	}
	time.Sleep(2 * time.Second)
}

WaitForAll


package main

import (
	"fmt"
	"github.com/yale8848/gorpool"
	"time"
)

func main() {

	p := gorpool.NewPool(5, 10).
		Start().
		EnableWaitForAll(true)
	for i := 0; i < 100; i++ {
		count := i
		p.AddJob(func() {
			time.Sleep(10 * time.Millisecond)
			fmt.Printf("%d\r\n", count)
		})
	}
	p.WaitForAll()
	p.StopAll()
}

SetIdleDuration

After set idle duration , the worker will stop it worker go routine


package main

import (
	"fmt"
	"github.com/yale8848/gorpool"
	"time"
)

func main() {

	p := gorpool.NewPool(5, 10).
		SetIdleDuration(3 * time.Second).
		Start().
		EnableWaitForAll(true)
	for i := 0; i < 100; i++ {
		count := i
		p.AddJob(func() {
			time.Sleep(5 * time.Second)
			fmt.Printf("%d\r\n", count)
		})
	}
	p.WaitForAll()
	p.StopAll()
}

Doc

gorpool doc

Documentation

Overview

Create by Yale 2018/1/8 14:47

Index

Constants

View Source
const MaxTime = time.Hour * 24 * 365 * 100

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job func()

Job function

type Pool

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

func NewPool

func NewPool(workerNum, jobNum int) *Pool

WorkerNum is worker number of worker pool,on worker have one goroutine

JobNum is job number of job pool

func (*Pool) AddJob

func (p *Pool) AddJob(job Job)

Add one job to job pool

func (*Pool) EnableWaitForAll

func (p *Pool) EnableWaitForAll(enable bool) *Pool

Enable whether WaitForAll

func (*Pool) SetIdleDuration

func (p *Pool) SetIdleDuration(idleTime time.Duration) *Pool

After idleTime stop a worker circulate, default 100 year

func (*Pool) Start

func (p *Pool) Start() *Pool

Start worker pool and dispatch

func (*Pool) StopAll

func (p *Pool) StopAll()

Stop all worker

func (*Pool) WaitForAll

func (p *Pool) WaitForAll()

Wait all job finish

func (*Pool) WorkerLength

func (p *Pool) WorkerLength() int

Jump to

Keyboard shortcuts

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