dispatch

package module
v0.0.0-...-24092ec Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2011 License: BSD-2-Clause Imports: 2 Imported by: 0

README

dispatch version 0.1_02

Package dispatch provides goroutine dispatch and concurrency limiting

About dispatch

Package dispatch provides an object Dispatch which is a queueing system for concurrent functions. It implements a dynamic limit on the number of routines it is runs simultaneously. It also implements an interface Queue, allowing for alternate queue implementations.

Performance

Generally, you run concurrent processes for increased program speed. So, you would like a dispatch method to be fast. However, the general purpose nature of the dispatch package causes some necessary bloating underlying the methods of Dispatch objects. For concurrent tasks which are doing actual work for more than a few hundred nanoseconds, this should not be very noticeable.

Hovever, if you have very high performance expectations, you may be better off writing your own lean and mean goroutine dispatcher that is suited for your individual purposes.

Dependencies

You must have Go installed (http://golang.org/).

Documentation

Installation

Use goinstall to install dispatch

goinstall github.com/bmatsuo/dispatch

Examples

You can usage examples by checking out the examples subdirectory. You can run the compile all the examples to run the locally with the command

cd $GOROOT/src/pkg/github.com/bmatsuo/dispatch && gomake exinstall && cd -

This installs all the examples. So, you can for instance run godu simply with the command

godu

When you are done, remove the examples with the command

cd $GOROOT/src/pkg/github.com/bmatsuo/dispatch && gomake exnuke && cd -

General Documentation

Use godoc to vew the documentation for dispatch

godoc github.com/bmatsuo/dispatch

Or alternatively, use a godoc http server

godoc -http=:6060

Then, visit the following URLs for complete dispatch documentation

Author

Bryan Matsuo [email protected]

Copyright (c) 2011, Bryan Matsuo. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Documentation

Overview

Package dispatch provides goroutine dispatch and concurrency limiting. It provides an object Dispatch which is a queueing system for concurrent functions. It implements a dynamic limit on the number of routines that it runs simultaneously. It also uses a Queue interface, allowing for alternate queue implementations.

See github.com/bmatsuo/dispatch/queues for more about the Queue interface and, a list of commonly used queues.

See github.com/bmatsuo/dispatch/examples for usage examples.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatch

type Dispatch struct {
	// The maximum number of goroutines can be changed while the queue is
	// processing.
	MaxGo int
	// contains filtered or unexported fields
}

A Dispatch is an automated function dispatch queue with a limited number of concurrent gorountines. The queue can be altered with the Dispatch methods Enqueue and SetKey (TODO add method Remove).

func New

func New(maxroutines int) *Dispatch

Create a new Dispatch object with a specified limit on concurrency.

func NewCustom

func NewCustom(maxroutines int, queue queues.Queue) *Dispatch

Create a new Dispatch object with a custom backend queue satisfying interface queues.Queue. It is not safe to allow non-Dispatch methods any access to the object queue. This can lead to race conditions with possible corruption of internal structures. So, it's considered a best practice to only pass NewCustom(...) newly created queues.

fifoDispatch     := NewCustom(10, queues.NewFIFO())
priorityDispatch := NewCustom(20, queues.NewPriorityQueue())

func (*Dispatch) Enqueue

func (gq *Dispatch) Enqueue(t queues.Task) int64

Enqueue a task for execution as a goroutine. The given queues.Task is given a unique id (int64) and stored in the Dispatch gq's backend queues.Queue object.

func (*Dispatch) Len

func (gq *Dispatch) Len() int

Returns the current length of the Dispatch object's queue.

func (*Dispatch) MaxLen

func (gq *Dispatch) MaxLen() int

Returns the maximum length attained by the Dispatch object's queue.

func (*Dispatch) Start

func (gq *Dispatch) Start()

Start executing goroutines. Don't stop until gq.Stop() is called. This method will take control of the calling thread. But, it's safe to call in a goroutine.

gq := dispatch.New()
go gq.Start()
wg := new(sync.WaitGroup)
for i := 0 ; i < 1000 ; i++ {
    wg.Add(1)
    gq.Enqueue(dispatch.NewTask(
        func(id int64) {
            log.Printf("I'm alive %d", i)
            wg.Done()
        } ) )
}
wg.Wait()
gq.Stop()

func (*Dispatch) Stop

func (gq *Dispatch) Stop()

Stop the queue after gq.Start() has been called. Any goroutines which have not already been dequeued will not be executed until gq.Start() is called again.

type StdTask

type StdTask struct {
	F func(id int64)
}

A simple task for use in a priority-less queue. See package github.com/bmatsuo/dispatch/queues

func NewTask

func NewTask(f func(int64)) *StdTask

Return the pointer to a newly allocated StdTask.

func (*StdTask) Func

func (dt *StdTask) Func() func(id int64)

Function modifier method for the queues.Task interface.

func (*StdTask) SetFunc

func (dt *StdTask) SetFunc(f func(id int64))

Function modifier method for the queues.Task interface.

func (*StdTask) Type

func (dt *StdTask) Type() string

Returns "StdTask" for the queues.Task interface.

Directories

Path Synopsis
Examples using the dispatch package.
Examples using the dispatch package.
Package queues defines the Queue interface used in package dispatch, and several Queue implementations.
Package queues defines the Queue interface used in package dispatch, and several Queue implementations.

Jump to

Keyboard shortcuts

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