Code Monkey home page Code Monkey logo

pool's Introduction

easy_go_pool 特性
1.动态扩容至指定size
2.size可异步改变:AdjustSize(workNum uint16)
3.可异步关闭也可同步等待任务完成后关闭
适合对任务处理时间短任务量有限的工作进行处理
go_pool 特性
1.动态扩容至指定size
2.size可异步改变:AdjustSize(workNum uint16)
3.当任务量小于需要时会按设置的间隔时间动态缩容
4.可异步关闭也可同步等待任务完成后关闭
适合对任务处理时间较长任务量不确定的工作进行处理
示例代码
package main

import (
	epool "github.com/zhaomin1993/pool/easy_go_pool"
	"log"
	"math/rand"
	"time"
)

type Score struct {
	Num int
}

func (s Score) Do() {
	if s.Num%10000 == 0 {
		log.Println("num:", s.Num)
	}
	if s.Num%2 == 0 {
		panic(s.Num)
	}
	time.Sleep(time.Millisecond * 100)
}

func main() {
	//创建协程池
	pool := epool.NewWorkerPool(1000, 1100)
	defer pool.Close() //关闭协程池
	pool.OnPanic(func(msg interface{}) {
		//log.Println("error:", msg)
	})
	datanum := 100 * 100 * 10
	for i := 1; i <= datanum; i++ {
		//接收任务
		if err := pool.Accept(Score{Num: i}); err != nil {
			log.Println("err:\t", err)
			break
		}
		if i%10000 == 0 {
			log.Println("send num:", i)
			randNum := rand.Intn(10) + 1000
			//调整协程池大小
			pool.AdjustSize(uint16(randNum))
		}
	}
}
benchmark 测试

1.png 2.png 3.png 4.jpg 5.png

pool's People

Contributors

zhaomin1993 avatar

Stargazers

 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.