workpool

package module
v0.0.0-...-a683769 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2016 License: BSD-2-Clause-Views Imports: 6 Imported by: 9

README

Workpool

Copyright 2013 Ardan Studios. All rights reserved.
Use of this source code is governed by a BSD-style license that can be found in the LICENSE handle.

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

Ardan Studios
12973 SW 112 ST, Suite 153
Miami, FL 33186
bill@ardanstudios.com

Click To View Documentation

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

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCapacity = errors.New("Thread Pool At Capacity")
)

Functions

This section is empty.

Types

type PoolWorker

type PoolWorker interface {
	DoWork(workRoutine int)
}

PoolWorker must be implemented by the object we will perform work on, now.

type WorkPool

type WorkPool struct {
	// contains filtered or unexported fields
}

WorkPool implements a work pool with the specified concurrency level and queue capacity.

func New

func New(numberOfRoutines int, queueCapacity int32) *WorkPool

New creates a new WorkPool.

func (*WorkPool) ActiveRoutines

func (workPool *WorkPool) ActiveRoutines() int32

ActiveRoutines will return the number of routines performing work.

func (*WorkPool) PostWork

func (workPool *WorkPool) PostWork(goRoutine string, work PoolWorker) (err error)

PostWork will post work into the WorkPool. This call will block until the Queue routine reports back success or failure that the work is in queue.

func (*WorkPool) QueuedWork

func (workPool *WorkPool) QueuedWork() int32

QueuedWork will return the number of work items in queue.

func (*WorkPool) Shutdown

func (workPool *WorkPool) Shutdown(goRoutine string) (err error)

Shutdown will release resources and shutdown all processing.

Jump to

Keyboard shortcuts

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