conc

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 3 Imported by: 0

README

conc

Package 'conc' runs (concurrently) the given task functions. Each function takes an 'onDispose' argument, the another function, to which one can pass handler, that will be called at the end of whole sequence.

See the package documentation for details.

Documentation

Overview

Example (Parallel)
conc.Run(
	func(onDispose dispose.It) error {
		fmt.Println("Hello #1.1")
		time.Sleep(10 * time.Millisecond)
		fmt.Println("Hello #1.2")
		time.Sleep(10 * time.Millisecond)
		onDispose(func() { fmt.Println("Dispose #1") })
		return nil
	},
	func(onDispose dispose.It) error {
		onDispose(func() { fmt.Println("Dispose #2") })
		time.Sleep(5 * time.Millisecond)
		fmt.Println("Hello #2.1")
		time.Sleep(10 * time.Millisecond)
		fmt.Println("Hello #2.2")
		return nil
	},
)
Output:

Hello #1.1
Hello #2.1
Hello #1.2
Hello #2.2
Dispose #2
Dispose #1
Example (Parallel_errors)
err := conc.Run(
	func(onDispose dispose.It) error {
		fmt.Println("Hello #1.1")
		return errors.New("Error #1")
	},
	func(onDispose dispose.It) error {
		time.Sleep(5 * time.Millisecond)
		fmt.Println("Hello #2.1")
		return errors.New("Error #2")
	},
)
fmt.Println(err)
Output:

Hello #1.1
Hello #2.1
Error #1
Error #2
Example (Single)
conc.Run(func(onDispose dispose.It) error {
	fmt.Println("Hello #1")
	onDispose(func() { fmt.Println("Dispose #1") })
	return nil
})
Output:

Hello #1
Dispose #1
Example (Tree)
err := conc.Run(
	func(onDispose dispose.It) error {
		fmt.Println("Hello #1.1")
		time.Sleep(10 * time.Millisecond)
		fmt.Println("Hello #1.2")
		time.Sleep(10 * time.Millisecond)
		onDispose(func() { fmt.Println("Dispose #1") })
		return nil
	},
	conc.Tasks(
		func(onDispose dispose.It) error {
			onDispose(func() { fmt.Println("Dispose #2") })
			time.Sleep(5 * time.Millisecond)
			fmt.Println("Hello #2.1")
			time.Sleep(10 * time.Millisecond)
			fmt.Println("Hello #2.2")
			return nil
		},
		func(onDispose dispose.It) error {
			time.Sleep(5 * time.Millisecond)
			onDispose(func() { fmt.Println("Dispose #3") })
			time.Sleep(9 * time.Millisecond)
			fmt.Println("Hello #3.1")
			time.Sleep(10 * time.Millisecond)
			fmt.Println("Hello #3.2")
			return nil
		},
	),
)
fmt.Println(err)
Output:

Hello #1.1
Hello #2.1
Hello #1.2
Hello #3.1
Hello #2.2
Hello #3.2
Dispose #2
Dispose #3
Dispose #1
<nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(task Task, moreTasks ...Task) error

Run executes (in parallel) the given tasks. It calls all collected disposers before return.

Types

type Task

type Task func(onDispose dispose.It) error

Task is a function that does some job. It can create resources that need to be disposed at the end of process. Disposers of these resources should be passed to the 'onDispose' function.

func Tasks

func Tasks(tasks ...Task) Task

Tasks creates a task from a several other tasks. This tasks will be run in parallel.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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