Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Go package for managing a group of goroutines that returns results iteratively in the order tasks are added.

License

Notifications You must be signed in to change notification settings

weirdname404/taskgroup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

taskgroup

GoDoc

A Go package for managing a group of goroutines that returns results iteratively in the order tasks are added.

Usage

Adding tasks to a taskgroup with context and limited concurrency

package main
import (
	"context"
	"fmt"
	"time"
	"github.com/weirdname404/taskgroup"
)
func taskThatUsesCtx(ctx context.Context) string {
	// something is happening that requires context ...
	time.Sleep(time.Duration(50 * time.Millisecond))
	return "taskgroup"
}
func task() string {
	// something is happening ...
	time.Sleep(time.Duration(200 * time.Millisecond))
	return "hello "
}
func main() {
	tgr := taskgroup.NewTaskGroup[string](context.TODO()).Limit(2)
	err := tgr.Add(task)
	if err != nil {
		panic(err)
	}
	err = tgr.Add(taskThatUsesCtx)
	if err != nil {
		panic(err)
	}
	// Start tasks
	res := ""
	for v := range tgr.Start() {
		res += v
	}
	err = tgr.Error()
	if err != nil {
		panic(err)
	}
	fmt.Println(res)
}

Result

hello taskgroup

Create taskgroup

// cancelling context stops all tasks in the taskgroup
ctx := context.TODO()
// 1. You must specify the type of the task results by the type parameter;
// 2. Concurrency limit is optional, but the default is NO LIMIT;
tgr := taskgroup.NewTaskGroup[string](ctx).Limit(2)

Add tasks

// adding tasks DOES NOT START them
err := tgr.Add(task)
// task may have an unsupported format, so it is better to check for errors
if err != nil {
 panic(err)
}
// you can add tasks that either require context or not
err = tgr.Add(taskThatUsesCtx)
if err != nil {
 panic(err)
}

Start taskgroup and check for errors

// Start tasks
res := ""
for v := range tgr.Start() {
 res += v
}
// at least one error stops the group;
err = tgr.Error()
if err != nil {
 panic(err)
}

Adding tasks to a taskgroup that can return an error

package main
import (
	"context"
	"errors"
	"fmt"
	"time"
	"github.com/weirdname404/taskgroup"
)
var TaskError = errors.New("Task error")
func task() string {
	return "hello "
}
func taskThatReturnsError() (string, error) {
	// something is happening ...
	time.Sleep(time.Duration(100 * time.Millisecond))
	return "", TaskError
}
func main() {
	tgr := taskgroup.NewTaskGroup[string](context.TODO()).Limit(2)
	err := tgr.Add(task)
	if err != nil {
		panic(err)
	}
	err = tgr.Add(taskThatReturnsError)
	if err != nil {
		panic(err)
	}
	// Start tasks
	res := ""
	for v := range tgr.Start() {
		res += v
	}
	err = tgr.Error()
	if err != nil {
		fmt.Printf("Taskgroup returns error - %s\n", err.Error())
	}
}

Result

Taskgroup returns error - Task error

About

A Go package for managing a group of goroutines that returns results iteratively in the order tasks are added.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /