pq

package module
v0.0.0-...-01f19f8 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2015 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Simple priority queue

==

  1. Create queue
  2. Start N workers
  3. Execute your tasks over workers (sync or async) Create groups of tasks (if you want)
  4. Make high priority for important tasks

Installation:

go get -u github.com/cheggaaa/pq

Example:

package main

import (
	"fmt"
	"github.com/cheggaaa/pq"
	"time"
)

type HardWork struct {
	name     string
	priority int
	duration int
}

// implement pq.Task
func (w *HardWork) Priority() int {
	return w.priority
}

func (w *HardWork) Run() (err error) {
	fmt.Printf("Start: %s (%d)\n", w.name, w.priority)
	time.Sleep(time.Duration(w.duration) * time.Second)
	fmt.Println("Done:", w.name)
	return
}

var ToDo = []pq.Task{
	&HardWork{
		name:     "Convert picture 1",
		priority: 5,
		duration: 1,
	},
	&HardWork{
		name:     "Convert picture 2",
		priority: 5,
		duration: 1,
	},
	&HardWork{
		name:     "Convert picture 3",
		priority: 5,
		duration: 1,
	},
	&HardWork{
		name:     "Convert picture 4",
		priority: 5,
		duration: 1,
	},
	&HardWork{
		name:     "Sing a song",
		priority: 100, // very important :-)
		duration: 10,
	},
	&HardWork{
		name:     "Convert video 1",
		priority: 50,
		duration: 5,
	},
	&HardWork{
		name:     "Convert video 2",
		priority: 55,
		duration: 5,
	},
}

func main() {
	// create queue
	q := &pq.Queue{}
	// start two workers
	q.Start(2)

	// add and wait ToDo
	fmt.Println("Add tasks...")
	q.WaitGroup(ToDo)
	fmt.Println("ToDo done!")

	// just function
	q.WaitFunc(func() error {
		fmt.Println("Just do this")
		return nil
	}, 0)

	// add one more task
	fmt.Println(".. and last task")
	q.WaitTask(&HardWork{
		name:     "Last mega task",
		duration: 1,
	})
	fmt.Println("Now all tasks is done, stop queue")
	q.Stop()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrQueueAlreadyStarted = fmt.Errorf("Queue already started")
View Source
var ErrQueueNotStarted = fmt.Errorf("Queue not started or closed")

Functions

This section is empty.

Types

type Queue

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

func (*Queue) AddFunc

func (q *Queue) AddFunc(f func() error, priority int) (err error)

Add func() to queue

func (*Queue) AddGroup

func (q *Queue) AddGroup(tasks []Task) (err error)

Just add group of tasks

func (*Queue) AddTask

func (q *Queue) AddTask(task Task) (err error)

Add single task to queue

func (*Queue) Len

func (q *Queue) Len() int

Size of queue

func (*Queue) Start

func (q *Queue) Start(numWorkers int) (err error)

Starts work. You can add tasks only after starting queue

func (*Queue) Stop

func (q *Queue) Stop()

Stopping queue. Wait while all workers finish current tasks

func (*Queue) TaskRunning

func (q *Queue) TaskRunning() int

How much workers do work at this moment

func (*Queue) WaitFunc

func (q *Queue) WaitFunc(f func() error, priority int) (err error)

Add func() to queue and wait while tasks will be done

func (*Queue) WaitGroup

func (q *Queue) WaitGroup(tasks []Task) (err error)

Add group of tasks and waits while all tasks will be done

func (*Queue) WaitTask

func (q *Queue) WaitTask(task Task) (err error)

Add single task to queue and waits while task will be done

type Task

type Task interface {
	Priority() int
	Run() (err error)
}

task interface

Jump to

Keyboard shortcuts

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