Skip to content
/ gool Public

Lightweight goroutine pool / 轻量级Golang协程池

License

Notifications You must be signed in to change notification settings

LgoLgo/gool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gool

Lightweight goroutine pool / 轻量级Golang协程池

Installation

To install this package, you need to install Go and set your Go workspace first.

  1. You first need Go installed, then you can use the below Go command to install gool.
go get -u github.com/LgoLgo/gool
  1. Import it in your code:
import "github.com/LgoLgo/gool"

Quick start

# assume the following codes in example.go file
$ cat example.go
package main

import (
	"fmt"
	"time"

	"github.com/LgoLgo/gool/pool"
)

func main() {
	// Create some tasks
	t := pool.NewTask(func() error {
		fmt.Println(time.Now())
		return nil
	})

	// Create a goroutine pool,
	// the maximum number of workers in this goroutine pool is 4
	p := pool.NewPool(4)

	taskNum := 0

	// Give these tasks to the goroutine pool
	go func() {
		for {
			// Continuously write task t to p,
			// each task is to print the current time
			p.EntryChannel <- t
			taskNum += 1
			fmt.Printf("%d tasks currently executed\n", taskNum)
		}
	}()

	// Start the pool and let the pool start working.
	// At this time, the pool will create a worker and let the worker work
	p.Run()
}

License

This project is under the Apache License 2.0. See the LICENSE file for the full license text.