taskgo

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

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

Go to latest
Published: Aug 7, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

taskgo

taskgo is a lightweight task pool in Go

Install

go get github.com/limpo1989/taskgo@latest

API Overview

type TaskExecutor[T any] interface {
	Exec(task T)
	Cancel(timeout time.Duration)
}

func NewTaskExecutor[T any](parent context.Context, concurrency int32, executor func(ctx context.Context, task T)) TaskExecutor[T]
func NewActionExecutor(parent context.Context, concurrency int32) TaskExecutor[func()]

Example

package main

import (
	"context"
	"sync"

	"github.com/limpo1989/taskgo"
)

func fib(n int) int {
	switch n {
	case 0, 1:
		return n
	default:
		return fib(n-1) + fib(n-2)
	}
}

func main() {
	ae := taskgo.NewActionExecutor(context.Background(), 16)

	const M = 10000
	const N = 20

	wg := &sync.WaitGroup{}
	wg.Add(M)

	for j := 0; j < M; j++ {
		ae.Exec(func() {
			fib(N)
			wg.Done()
		})
	}

	wg.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TaskExecutor

type TaskExecutor[T any] interface {
	Exec(task T)
	Cancel(timeout time.Duration)
}

func NewActionExecutor

func NewActionExecutor(parent context.Context, concurrency int32) TaskExecutor[func()]

func NewTaskExecutor

func NewTaskExecutor[T any](parent context.Context, concurrency int32, executor func(ctx context.Context, task T)) TaskExecutor[T]

Jump to

Keyboard shortcuts

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