async

package module
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2025 License: MIT Imports: 6 Imported by: 1

README

async

GoReference

This package provides a utility to run async task.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChanCtx added in v1.3.0

func ChanCtx[T any](ctx context.Context, ch <-chan T) iter.Seq[T]

ChanCtx return iterator function that will yield values in the ch or until ctx is done.

similarity can be seen in the following code, but the later can be canceled by the context

for value := range ch {
	// ...
}

for value := range async.ChanCtx(ctx, ch) {
	// ...
}

func PoolRun2 added in v1.5.0

func PoolRun2[R any](p *Pool, f func() (R, error)) <-chan Result[R]

PoolRun2 similar to Pool.Run but also returning other value not just error.

func Promise added in v1.8.0

func Promise[R any](f func() (R, error)) func(context.Context) (R, error)

Run f in new goroutine, return a function that can be used concurrently to get the result, if f panic, the error value pass to panic will be returned.

func RecvCtx added in v1.7.0

func RecvCtx[T any](ctx context.Context, c <-chan T) (value T, ok bool)

RecvCtx waits for a value from the channel or until the context is done.

func Run

func Run(f func() error) <-chan error

Run the f function in new go routine, and return chan to get the value returned by f or the panic value if f panic.

func Run2

func Run2[R any](f func() (R, error)) <-chan Result[R]

Run2 similar with Run but also returning other value not just error.

func SendCtx added in v1.7.0

func SendCtx[T any](ctx context.Context, c chan<- T, value T) bool

SendCtx sends a value to the channel or returns false if the context is done.

func WaitGroupRun2 added in v1.7.0

func WaitGroupRun2[R any](wg *WaitGroup, f func() (R, error)) <-chan Result[R]

WaitGroupRun2 similar to WaitGroup.Run but also returning other value not just error.

Types

type Mutex added in v1.6.0

type Mutex struct{ sync.Mutex }

func (*Mutex) Run added in v1.6.0

func (m *Mutex) Run(f func())

Run runs a function with mutex control.

func (*Mutex) RunE added in v1.12.0

func (m *Mutex) RunE(f func() error) error

func (*Mutex) RunENoPanic added in v1.12.0

func (m *Mutex) RunENoPanic(f func() error) error

func (*Mutex) RunNoPanic added in v1.8.0

func (m *Mutex) RunNoPanic(f func())

RunNoPanic is similar to Mutex.Run but assuming f will not panic.

if f panic, the mutex will not be unlocked.

type Pool added in v1.4.0

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

func NewPool added in v1.4.0

func NewPool(ctx context.Context, size int) *Pool

NewPool creates a new goroutine pool with the specified size.

func (*Pool) Close added in v1.4.0

func (p *Pool) Close()

Close closes the pool and waits for all goroutines to finish.

func (*Pool) Run added in v1.6.0

func (p *Pool) Run(f func() error) <-chan error

Run submits a function to the pool, and return chan to get the value returned by f or the panic value if f panic.

type RWMutex added in v1.7.0

type RWMutex struct{ sync.RWMutex }

func (*RWMutex) Run added in v1.7.0

func (m *RWMutex) Run(f func())

Run runs a function with mutex control.

func (*RWMutex) RunE added in v1.12.0

func (m *RWMutex) RunE(f func() error) error

func (*RWMutex) RunENoPanic added in v1.12.0

func (m *RWMutex) RunENoPanic(f func() error) error

func (*RWMutex) RunERead added in v1.12.0

func (m *RWMutex) RunERead(f func() error) error

func (*RWMutex) RunEReadNoPanic added in v1.12.0

func (m *RWMutex) RunEReadNoPanic(f func() error) error

func (*RWMutex) RunNoPanic added in v1.8.0

func (m *RWMutex) RunNoPanic(f func())

RunNoPanic is similar to RWMutex.Run but assuming f will not panic.

if f panic, the mutex will not be unlocked.

func (*RWMutex) RunRead added in v1.7.0

func (m *RWMutex) RunRead(f func())

RunRead runs a function with mutex control for read-only data.

func (*RWMutex) RunReadNoPanic added in v1.8.0

func (m *RWMutex) RunReadNoPanic(f func())

RunReadNoPanic is similar to RWMutex.RunRead but assuming f will not panic.

if f panic, the mutex will not be unlocked.

type Result

type Result[R any] struct {
	Result R
	Error  error
}

type Sem added in v1.5.0

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

func NewSem added in v1.5.0

func NewSem(size int) Sem

func (Sem) Run added in v1.6.0

func (s Sem) Run(f func())

Run runs a function with semaphore control.

func (Sem) RunNoPanic added in v1.8.0

func (s Sem) RunNoPanic(f func())

RunNoPanic is similar to Sem.Run but assuming f will not panic.

if f panic, the semaphore count will not be restored.

type WaitGroup added in v1.1.0

type WaitGroup struct{ sync.WaitGroup }

func (*WaitGroup) Go added in v1.1.0

func (wg *WaitGroup) Go(f func())

Go is similar to normal go keyword, but it registers to the waitgroup.

func (*WaitGroup) Run added in v1.6.0

func (wg *WaitGroup) Run(f func() error) <-chan error

Run f in new goroutine, and register it into the waitgroup, and return chan to get the value returned by f or the panic value if f panic.

Jump to

Keyboard shortcuts

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