async

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: MIT Imports: 3 Imported by: 0

README

Async

Usage

import "github.com/xxddpac/async"

//create a default async pool(maxWorkers=100, maxQueue=100)
p := async.New()

// or create a async pool with options
// p := async.New(async.WithMaxWorkers(), async.WithMaxQueue(),async.WithOnError(func(err error) {}))

// close the pool when you don't need it
defer p.Close()

// add tasks to the pool
for i := 0; i < 10000; i++ {
    p.Wg.Add(1)
    p.Add(func(args ...interface{}) error {
	defer p.Wg.Done()
	// do something
	return nil
	})
	
// wait for all tasks to finish
p.Wg.Wait()

Documentation

Index

Constants

This section is empty.

Variables

Functions

func NewWorker

func NewWorker(id int, pool *WorkerPool) *worker

Types

type Job

type Job func(args ...interface{}) error

type JobWithArgs

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

type Logger

type Logger interface {
	Printf(string, ...interface{})
}

type LoggerFunc

type LoggerFunc func(string, ...interface{})

func (LoggerFunc) Printf

func (f LoggerFunc) Printf(msg string, args ...interface{})

type Option

type Option func(*WorkerPool)

func WithLogger

func WithLogger(logger Logger) Option

func WithMaxQueue

func WithMaxQueue(count int) Option

func WithMaxWorkers

func WithMaxWorkers(count int) Option

func WithOnError added in v1.0.2

func WithOnError(fn func(err error)) Option

type Status

type Status int
const (
	Idle Status = iota + 1
	Busy
	Closed
)

type WorkerPool

type WorkerPool struct {
	Logger Logger

	Wg sync.WaitGroup
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *WorkerPool

func (*WorkerPool) Add added in v1.0.2

func (w *WorkerPool) Add(fn Job, args ...interface{})

func (*WorkerPool) Close

func (w *WorkerPool) Close()

func (*WorkerPool) Run

func (w *WorkerPool) Run() *WorkerPool

func (*WorkerPool) WorkerCount

func (w *WorkerPool) WorkerCount() int

Jump to

Keyboard shortcuts

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