gwp

package module
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2022 License: MIT Imports: 7 Imported by: 2

README

gwp

Simple goroutine worker pool written in Go.

Status: Work in progress.

Example:

package main

import (
	"log"
	"time"

	"github.com/nxshock/gwp"
)

// Job function
func f(i int) error {
	log.Printf("Job №%d", i)

	// Simulate work
	time.Sleep(time.Second)

	return nil
}

func main() {

	worker := gwp.New(4)         // Create pool with specified number of workers

	worker.ShowProgress = true   // Enable progress indicator
	worker.ShowSpeed = true      // Show processing speed in progress indicator
	worker.SetEstimateCount(100) // Set total number on jobs to calculate ETA

	for i := 0; i < 100; i++ {
		n := i
		// Send job
		worker.Add(func() error {
			return f(n)
		})
	}

	// Wait all jobs to complete
	worker.CloseAndWait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WorkerPool

type WorkerPool struct {
	ShowProgress bool
	ShowSpeed    bool
	// contains filtered or unexported fields
}

WorkerPool represents pool of workers.

func New

func New(threadCount int) *WorkerPool

New creates new pool of workers with specified goroutine count. If specified number of workers less than 1, runtime.NumCPU() is used.

func (*WorkerPool) Add

func (workerPool *WorkerPool) Add(f func() error)

Add sends specified task for execution.

func (*WorkerPool) CloseAndWait

func (workerPool *WorkerPool) CloseAndWait()

CloseAndWait stops accepting tasks and waits for all tasks to complete.

func (*WorkerPool) ErrorCount added in v0.1.1

func (workerPool *WorkerPool) ErrorCount() int

ErrorCount returns total error count.

func (*WorkerPool) SetEstimateCount added in v0.1.9

func (workerPool *WorkerPool) SetEstimateCount(n int)

Jump to

Keyboard shortcuts

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