parallel

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

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

Go to latest
Published: May 11, 2016 License: GPL-3.0 Imports: 4 Imported by: 1

README

parallel executor for golang

This package should be deprecated in favor of github.com/wushilin/threads

Install

go get github.com/wushilin/parallel

Documentation

$ godoc -http=":16666"

To view documentation, please browse http://localhost:16666

Usage

parallel

Deprecated, not going to explain. See parallel_test.go for examples

Documentation

Overview

Provide parallel executor service

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeFutureCallPipe

func MakeFutureCallPipe(f MapCallFunc, argument interface{}) (FutureCall, VoidFunc)

Same as MakeFuturePipe, but the result FutureCall doesn't carry value

In fact it is impemented using MakeFuturePipe with wrapper func

func MakeFuturePipe

func MakeFuturePipe(f MapFunc, argument interface{}) (Future, VoidFunc)

Apply function f with argument argument But will not run it. Instead, it return the future immediately and a VoidFunc (func (){}). Caller can call the VoidFunc() to Cause the Future to materialize.

Example:

fut, f := MakeFuturePipe(func(i interface{}) interface{} {
  return i.(int) + 1
}, 6)
go f() <= this will start the actual func - without it the fut will never be ready
fmt.Println(fut.Wait())

Note the f() is required before the fut.Wait()

Types

type Future

type Future interface {
	// Test wehther the value is ready
	Ready() bool

	/*
		Wait for max of timeout milliseconds
		for a value and return whether a value present or not
		If second return value is false, the value returned
		should be nil. Do not use the value unless the second
		value is true
	*/
	WaitT(timeoutms int) (interface{}, bool)

	// Wait indefinitely for the value
	Wait() interface{}
}

Future represent a Value to be retrieved, and might be already ready, or will be ready in the future.

func MakeFuture

func MakeFuture(f MapFunc, argument interface{}) Future

Make a function call as a future. A separate goroutine will be started to execute the code It is the same as:

fut, pipe := MakeFuturePipe(f, argument)
go pipe()

You can retrieve the result Future in caller goroutine sometime in the future

type FutureCall

type FutureCall interface {
	// test whether the future is ready
	Ready() bool
	// Wait max X ms, return true if the call is done. Otherwise return false
	WaitT(timeoutms int) bool
	// Wait until the call is done
	Wait()
}

A FutureCall represent a future completion state of a Call. Call Ready() to determin if he Call is done Call WaitT(xxx int) to check if the call is complete after the timeout Note if the call returns earlier than timeout, the WaitT will return immediately Call Wait() to wait indefinitely for the call to finish.

func MakeFutureCall

func MakeFutureCall(f MapCallFunc, argument interface{}) FutureCall

Same as MakeFuture, but the result FutureCall doesn't carry value

In fact it is impemented using MakeFutureCallPipe with wrapper func

type MapCallFunc

type MapCallFunc func(x interface{})
It is like a map function, but produce no output

a->fmt.Println(a) is a MapCallFunc

type MapFunc

type MapFunc func(x interface{}) interface{}

A Map func tranform value

type ParallelExecutor

type ParallelExecutor interface {
	/*
		Start processing jobs. Note that jobs can be added before Start()

		but without start, Future will never materialize

		You can still add jobs after started. You can start before adding jobs even
	*/
	Start()

	/*
		Submit a producer func and get the result.

		The Future may be ready in the future. Or might be ready when you use it

		call result.Ready() to check whether it is completed

		call result.WaitT to wait for completion, with a timeout

		call result.Wait to wait forever for completion
	*/
	Submit(f interface{}) Future

	/*
		Submit a void func for executing.

		The result represent a Future Completion status of the VoidFunc

		call result.Ready() to check whether it is completed

		call result.WaitT to wait for completion, with a timeout

		call result.Wait to wait forever for completion
	*/
	Call(f interface{}) FutureCall

	/*
		Stop accepting new calls, but all submitted tasks will be executed

		It is essentially closing the task queue channel
	*/
	Stop()

	/*
		Wait until all calls/funcs are done

		You must call Stop() first otherwise the goroutines will never end
	*/
	Wait()

	/*
		Active threads.
		Before start, it should be 0

		After start, it will quickly jump to Parallel settings

		After stop, it will slowly drop to 0.

		Use as a indicator only, don't assume 0 means the

		Executor done all jobs. Instead, use Stop() and Wait()
	*/
	Active() int

	// Pending jobs count. It is len(queue)
	Pending() int

	// Return parallel configuration. You can't adjust the threads dynamically
	Parallel() int

	// Test wehther the executor is running
	Running() bool

	// Total submited tasks
	Submited() int

	// Number of tasks had been completed
	Completed() int
}

A Parallel Executor is an executor that can execute tasks in parallel

func NewExecutor

func NewExecutor(p int, qlength int) ParallelExecutor

Return a parallel executor p is number of concurrent goroutines. Don't set too high or your machine will hang? qlength is the queue length. If this is small, when caller want to submit a task, but the queue is full, the caller will be blocked until a slot is available

After executor is closed, pending caller in submission queue will see panic But already successfully submitted tasks will be executed. All Future and FutureCall will guarantee a termination with result.

type ProducerFunc

type ProducerFunc func() interface{}

ProducerFunc is a func that produces a result

type VoidFunc

type VoidFunc func()

A void func is a func that takes no argument, and returns nothing

Jump to

Keyboard shortcuts

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