asyncio

package module
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 5 Imported by: 2

README

asyncio

并发异步库

使用
package asyncio_test

import (
	"fmt"
	"math"
	"testing"
	"time"

	"github.com/Drelf2018/asyncio"
)

func sleep(second int) float64 {
	time.Sleep(time.Duration(second) * time.Second)
	return math.Sqrt(float64(second))
}

type Student struct {
	Name string
}

func (s Student) Introduce(more string) {
	time.Sleep(time.Duration(2) * time.Second)
	println("My name is", s.Name, "and", more)
}

func (s Student) Hello() {
	print("Hello ")
}

func (s Student) Me() {
	time.Sleep(time.Second)
	println(s.Name)
}

func TestSleep(t *testing.T) {
	handles := asyncio.Slice(asyncio.SingleArg(1, 2, 3, 4), sleep)

	// need hint type
	a := asyncio.To[[]float64](handles)
	fmt.Printf("a: %v\n", a)
	// auto infer
	b := make([]float64, handles.Len())
	asyncio.To(handles, b)
	fmt.Printf("b: %v\n", b)

	for i, handle := range handles {
		fmt.Printf("No.%v sleep() return %v\n", i, handle.Result())
	}
}

func TestStruct(t *testing.T) {
	s := Student{"Alice"}
	coro := asyncio.C(s.Introduce, "I'm glad to see you!")
	coros := asyncio.NoArgsFunc(s.Hello, s.Me)
	asyncio.Await(append(coros, coro)...)
}
控制台
// TestSleep
a: [1 1.4142135623730951 1.7320508075688772 2]
b: [1 1.4142135623730951 1.7320508075688772 2]
No.0 sleep() return [1]
No.1 sleep() return [1.4142135623730951]
No.2 sleep() return [1.7320508075688772]
No.3 sleep() return [2]
// TestStruct
Hello Alice
My name is Alice and I'm glad to see you!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoResult       = errors.New("task has no rusult")
	ErrNoSecondResult = errors.New("task has no second rusult")
)
View Source
var NewEventLoop = func() EventLoop {
	return &AbstractEventLoop{
		status: Stop,
		tasks:  sync.WaitGroup{},
	}
}

Functions

func Delay added in v0.13.0

func Delay(seconds float64, fn any, args ...any)

func Fill added in v0.12.0

func Fill[S ~[]E, E any](tasks []*Task, s S, position ...int)

func ForEach added in v0.7.0

func ForEach[S ~[]E, E any](args S, f func(E))

func ForEachPtr added in v0.12.0

func ForEachPtr[S ~[]E, E any](args S, f func(*E))

func ForEachPtrV added in v0.13.0

func ForEachPtrV[S ~[]E, E any, V any](args S, f func(*E) V) []V

func ForEachV added in v0.13.0

func ForEachV[S ~[]E, E any, V any](args S, f func(E) V) []V

func ForFunc added in v0.12.1

func ForFunc[E any](arg E, f ...func(E))

func ForFuncV added in v0.13.0

func ForFuncV[E any, V any](arg E, f ...func(E) V) []V

func Loop added in v0.12.0

func Loop(f func(loop EventLoop))

func Map

func Map[M ~map[K]V, K comparable, V any](m M, f func(K, V))

func MapR added in v0.13.0

func MapR[M ~map[K]V, K comparable, V any, R any](m M, f func(K, V) R) []R

func Promise added in v0.12.0

func Promise(fn any, args ...any) (p *promise)

func PromiseTask added in v0.12.0

func PromiseTask(task *Task) *promise

func Results added in v0.12.0

func Results[E any](tasks []*Task, position ...int) []E

func Retry

func Retry(times int, delay float64, f func() bool) bool

重试函数

times: 重试次数 负数则无限制

delay: 休眠秒数 每次重试间休眠时间

f: 要重试的函数

func RetryError

func RetryError(times int, delay float64, f func() error) bool

重试函数 通过是否抛出 error 判断

func RetryErrorTask added in v0.12.0

func RetryErrorTask(times int, delay float64, task *Task) bool

重试函数 通过是否抛出 error 判断 支持参数

func RetryTask added in v0.12.0

func RetryTask(times int, delay float64, task *Task) bool

重试函数 支持参数

func RunForever added in v0.12.0

func RunForever(tasks ...*Task)

func RunUntilComplete added in v0.12.0

func RunUntilComplete(tasks ...*Task)

func SetEventLoop

func SetEventLoop(fn func() EventLoop)

func Wait added in v0.7.0

func Wait(tasks ...*Task)

func WaitGroup added in v0.12.0

func WaitGroup(delta int, f func(done func()))

Types

type AbstractEventLoop

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

func (*AbstractEventLoop) CreateTask

func (loop *AbstractEventLoop) CreateTask(fn any, args ...any) (r *Task)

func (*AbstractEventLoop) Finish added in v0.12.0

func (loop *AbstractEventLoop) Finish()

func (*AbstractEventLoop) Run added in v0.7.0

func (loop *AbstractEventLoop) Run(task *Task)

func (*AbstractEventLoop) RunForever

func (loop *AbstractEventLoop) RunForever(tasks ...*Task)

func (*AbstractEventLoop) RunUntilComplete

func (loop *AbstractEventLoop) RunUntilComplete(tasks ...*Task)

func (*AbstractEventLoop) Start added in v0.12.0

func (loop *AbstractEventLoop) Start()

func (*AbstractEventLoop) Stop added in v0.12.0

func (loop *AbstractEventLoop) Stop()

type EventLoop added in v0.12.0

type EventLoop interface {
	CreateTask(fn any, args ...any) *Task
	Run(task *Task)

	Start()
	RunUntilComplete(tasks ...*Task)

	RunForever(tasks ...*Task)
	Stop()
}

func GetEventLoop

func GetEventLoop() EventLoop

type Status added in v0.12.0

type Status int
const (
	Stop Status = iota
	Running
)

type Task

type Task struct {
	Func reflect.Value
	Args []reflect.Value
	// contains filtered or unexported fields
}

func CreateTask added in v0.12.0

func CreateTask(fn any, args ...any) *Task

func Slice

func Slice[S ~[]E, E any](args S, f any) (tasks []*Task)

func (*Task) AddDoneCallback added in v0.12.0

func (t *Task) AddDoneCallback(c func(*Task)) *Task

func (*Task) Bool added in v0.5.0

func (t *Task) Bool() bool

func (*Task) Bool2 added in v0.12.0

func (t *Task) Bool2() bool

func (*Task) Check added in v0.12.0

func (t *Task) Check() bool

func (*Task) Check2 added in v0.12.0

func (t *Task) Check2() bool

func (*Task) Copy added in v0.12.0

func (t *Task) Copy(args ...any) *Task

func (*Task) Default added in v0.12.0

func (t *Task) Default(args ...any) *Task

func (*Task) Delay added in v0.13.0

func (t *Task) Delay(seconds float64) *Task

func (*Task) Done added in v0.12.0

func (t *Task) Done() bool

func (*Task) Error added in v0.5.0

func (t *Task) Error() error

func (*Task) Error2 added in v0.12.0

func (t *Task) Error2() error

func (*Task) First added in v0.12.0

func (t *Task) First() any

func (*Task) Reflect added in v0.12.0

func (t *Task) Reflect(args reflect.Value) *Task

func (*Task) Result added in v0.12.0

func (t *Task) Result() []any

func (*Task) Run added in v0.7.0

func (t *Task) Run() *Task

func (*Task) Second added in v0.12.0

func (t *Task) Second() any

Jump to

Keyboard shortcuts

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