task

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: MIT Imports: 2 Imported by: 0

README

taskpool

Coverage

CI Coverage Update release version.

Developer tools

For macOs
$ brew install go-task/tap/go-task
$ brew install golangci-lint
$ go install github.com/oligot/go-mod-upgrade@latest
$ brew install jq

Table of contents

Installation

go get -u github.com/arielsrv/taskpool

⚡️ Quickstart

package main

import (
	"github.com/arielsrv/taskpool"
	"log"
	"math/rand"
	"runtime"
	"time"
)

func main() {
	// generics task, return error is mandatory, @todo: fire and forget
	var task1, task2, task3 *task.Task[int]

	tb := &task.Builder{
		MaxWorkers: runtime.NumCPU() - 1,
	}

	start := time.Now()
	tb.ForkJoin(func(c *task.Awaitable) {
		task1 = task.Await[int](c, GetNumber)
		task2 = task.Await[int](c, GetNumber)
		task3 = task.Await[int](c, GetNumber)
	})

	log.Println(task1.Result)
	log.Println(task2.Result)
	log.Println(task3.Result)

	end := time.Since(start)
	log.Println(end)
}

func GetNumber() (int, error) {
	value := rand.Int()
	time.Sleep(time.Millisecond * 1000)
	return value, nil
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Awaitable

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

type Builder

type Builder struct {
	MaxWorkers  int
	MaxCapacity int
}

Builder should be singleton or run in your entry point MaxWorkers max parallel task MaxCapacity max queued task non-blocking

func (*Builder) ForkJoin

func (tb *Builder) ForkJoin(f func(*Awaitable))

ForkJoin execute a function task list with fixed worked.

type Task

type Task[T any] struct {
	Result T
	Err    error
}

func Await

func Await[T any](c *Awaitable, f func() (T, error)) *Task[T]

Await provides function to run

Jump to

Keyboard shortcuts

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