项目作者: gammazero

项目描述 :
Concurrency limiting goroutine pool
高级语言: Go
项目地址: git://github.com/gammazero/workerpool.git
创建时间: 2016-05-17T14:32:06Z
项目社区:https://github.com/gammazero/workerpool

开源协议:MIT License

下载


workerpool

GoDoc
Build Status
Go Report Card
codecov
License

Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting tasks, no matter how many tasks are queued.

This implementation builds on ideas from the following:

Installation

To install this package, you need to setup your Go workspace. The simplest way to install the library is to run:

  1. $ go get github.com/gammazero/workerpool

Example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gammazero/workerpool"
  5. )
  6. func main() {
  7. wp := workerpool.New(2)
  8. requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}
  9. for _, r := range requests {
  10. r := r
  11. wp.Submit(func() {
  12. fmt.Println("Handling request:", r)
  13. })
  14. }
  15. wp.StopWait()
  16. }

Example wrapper function to show start and finish time of submitted function.

Usage Note

There is no upper limit on the number of tasks queued, other than the limits of system resources. If the number of inbound tasks is too many to even queue for pending processing, then the solution is outside the scope of workerpool. It should be solved by distributing workload over multiple systems, and/or storing input for pending processing in intermediate storage such as a file system, distributed message queue, etc.