thread_pooling

command
v0.0.0-...-54e4ceb Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package workpool implements a pool of go routines that are dedicated to processing work that is posted into the pool.

Read the following blog post for more information:blogspot
http://www.goinggo.net/2013/05/thread-pooling-in-go-programming.html

New Parameters The following is a list of parameters for creating a TraceLog:

numberOfRoutines: Sets the number of worker routines that are allowed to process work concurrently
queueCapacity:    Sets the maximum number of pending work objects that can be in queue

WorkPool Management Go routines are used to manage and process all the work. A single Queue routine provides the safe queuing of work. The Queue routine keeps track of the amount of work in the queue and reports an error if the queue is full. The concurrencyLevel parameter defines the number of work routines to create. These work routines will process work subbmitted to the queue. The work routines keep track of the number of active work routines for reporting. The PostWork method is used to post work into the ThreadPool. This call will block until the Queue routine reports back success or failure that the work is in queue. Example Use Of ThreadPool The following shows a simple test application

package main
import (
    "github.com/goinggo/workpool"
    "bufio"
    "fmt"
    "os"
    "runtime"
    "strconv"
    "time"
)
type MyWork struct {
    Name      string "The Name of a person"
    BirthYear int    "The Yea the person was born"
    WP        *workpool.WorkPool
}
func (workPool *MyWork) DoWork(workRoutine int) {
    fmt.Printf("%s : %d\n", workPool.Name, workPool.BirthYear)
    fmt.Printf("*******> WR: %d  QW: %d  AR: %d\n", workRoutine, workPool.WP.QueuedWork(), workPool.WP.ActiveRoutines())
    time.Sleep(100 * time.Millisecond)
    //panic("test")
}
func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    workPool := workpool.New(runtime.NumCPU() * 3, 10)
    shutdown := false // Just for testing, I Know
    go func() {
		for i := 0; i < 1000; i++ {
			work := &MyWork{
				Name: "A" + strconv.Itoa(i),
				BirthYear: i,
				WP: workPool,
			}
			err := workPool.PostWork("name_routine", work)
			if err != nil {
				fmt.Printf("ERROR: %s\n", err)
				time.Sleep(100 * time.Millisecond)
			}
			if shutdown == true {
				return
			}
		}
	}()
    fmt.Println("Hit any key to exit")
    reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')
	shutdown = true
	fmt.Println("Shutting Down\n")
	workPool.Shutdown("name_routine")
}

Example Output The following shows some sample output

A336 : 336
******> QW: 5  AR: 8
A337 : 337
*******> QW: 4  AR: 8
ERROR: Thread Pool At Capacity
A338 : 338
*******> QW: 3  AR: 8
A339 : 339
*******> QW: 2  AR: 8
CHANGE FOR ARTICLE

Jump to

Keyboard shortcuts

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