Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shettyh/threadpool
Golang simple thread pool implementation
https://github.com/shettyh/threadpool
channels future golang scalable scheduler threadpool
Last synced: about 2 months ago
JSON representation
Golang simple thread pool implementation
- Host: GitHub
- URL: https://github.com/shettyh/threadpool
- Owner: shettyh
- License: apache-2.0
- Created: 2017-09-06T18:45:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-23T11:51:49.000Z (almost 5 years ago)
- Last Synced: 2024-07-31T20:51:54.928Z (5 months ago)
- Topics: channels, future, golang, scalable, scheduler, threadpool
- Language: Go
- Size: 36.1 KB
- Stars: 101
- Watchers: 5
- Forks: 21
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - threadpool - Golang threadpool implementation. (Goroutines / Search and Analytic Databases)
- awesome-go - threadpool - Golang simple thread pool implementation - ★ 6 (Goroutines)
- awesome-go-extra - threadpool - 09-06T18:45:39Z|2020-03-23T11:51:49Z| (Goroutines / Advanced Console UIs)
README
# Golang Threadpool implementation
[![Build Status](https://travis-ci.org/shettyh/threadpool.svg?branch=master)](https://travis-ci.org/shettyh/threadpool)
[![codecov](https://codecov.io/gh/shettyh/threadpool/branch/master/graph/badge.svg)](https://codecov.io/gh/shettyh/threadpool)
[![GoDoc](https://godoc.org/github.com/shettyh/threadpool?status.svg)](https://godoc.org/github.com/shettyh/threadpool)
[![Go Report Card](https://goreportcard.com/badge/github.com/shettyh/threadpool)](https://goreportcard.com/report/github.com/shettyh/threadpool)Scalable threadpool implementation using Go to handle the huge network trafic.
## Install
`go get github.com/shettyh/threadpool`
## Usage
### Threadpool
- Implement `Runnable` interface for tha task that needs to be executed. For example```
type MyTask struct { }
func (t *MyTask) Run(){
// Do your task here
}
```
- Create instance of `ThreadPool` with number of workers required and the task queue size
```
pool := threadpool.NewThreadPool(200,1000000)
```
- Create Task and execute
```
task:=&MyTask{}
err := pool.Execute(task)
```
- Using `Callable` task
```
type MyTaskCallable struct { }
func (c *MyTaskCallable) Call() interface{} {
//Do task
return result
}
//Execute callable task
task := &MyTaskCallable{}
future, err := pool.ExecuteFuture(task)
//Check if the task is done
isDone := future.IsDone() // true/false
//Get response , blocking call
result := future.Get()
```
- Close the pool
```
pool.Close()
```### Scheduled threadpool
- Create instance of `ScheduledThreadPool` with number of workers required
```
schedulerPool:= threadpool.NewScheduledThreadPool(10)
```
- Create Task and schedule
```
task:=&MyTask{}
pool.ScheduleOnce(task, time.Second*20) // Time delay is in seconds only as of now
```
- Close the pool
```
pool.Close()
```