asyncio

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MIT Imports: 5 Imported by: 3

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)...)
}

func TestAsyncEvent(t *testing.T) {
	a := make(asyncio.AsyncEvent)

	a.OnCommand("danmaku114", func(e *asyncio.Event) {
		data := e.Data()
		fmt.Printf("data: %v(%T)\n", data, data)
	})

	a.OnRegexp(`danmaku\d`, func(e *asyncio.Event) {
		e.Set("test", 3.14)
		test := e.Get("test")
		fmt.Printf("test: %v(%T)\n", test, test)
	})

	a.All(
		func(e *asyncio.Event) { fmt.Printf("e.Cmd(): %v\n", e.Cmd()) },
		func(e *asyncio.Event) { e.Abort() },
		func(e *asyncio.Event) { fmt.Println("Not stop") },
	)

	a.Dispatch("danmaku114", 514)
}
// 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!
// TestAsyncEvent
data: 514(int)
e.Cmd(): __ALL__
test: 3.14(float64)
PASS
ok      github.com/Drelf2018/asyncio    6.060s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

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

重试函数

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

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

f: 要重试的函数

func RetryError

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

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

func RetryErrorWith

func RetryErrorWith(times, delay int, coro Coro)

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

func RetryWith

func RetryWith[T any](times, delay int, coro Coro)

重试函数 支持参数

func SetEventLoop

func SetEventLoop(loop *AbstractEventLoop)

func To

func To[S ~[]T, T any](H H, ts ...S) []T

Types

type AbstractEventLoop

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

func GetEventLoop

func GetEventLoop() *AbstractEventLoop

func NewEventLoop

func NewEventLoop() *AbstractEventLoop

func (*AbstractEventLoop) Coro

func (loop *AbstractEventLoop) Coro(f any, args ...any) *Handle

func (*AbstractEventLoop) CreateTask

func (loop *AbstractEventLoop) CreateTask(coro Coro) *Handle

func (*AbstractEventLoop) RunForever

func (loop *AbstractEventLoop) RunForever()

func (*AbstractEventLoop) RunUntilComplete

func (loop *AbstractEventLoop) RunUntilComplete()

type Args

type Args []any

func SingleArg

func SingleArg[T any](args ...T) []Args

type AsyncEvent added in v0.4.0

type AsyncEvent map[string]model

func (AsyncEvent) All added in v0.4.0

func (a AsyncEvent) All(handles ...func(*Event)) (delete func())

func (AsyncEvent) Dispatch added in v0.4.0

func (a AsyncEvent) Dispatch(cmd string, data any)

func (AsyncEvent) Heartbeat added in v0.5.0

func (a AsyncEvent) Heartbeat(initdead, keepalive int, f func(stop func()))

func (AsyncEvent) On added in v0.4.0

func (a AsyncEvent) On(name, cmd string, handles ...func(*Event)) func()

func (AsyncEvent) OnCommand added in v0.4.0

func (a AsyncEvent) OnCommand(cmd string, handles ...func(*Event)) (delete func())

func (AsyncEvent) OnRegexp added in v0.4.0

func (a AsyncEvent) OnRegexp(pattern string, handles ...func(*Event)) (delete func())

func (AsyncEvent) Register added in v0.4.0

func (a AsyncEvent) Register(name string, choose func(cmd, key string) bool)

func (AsyncEvent) Registered added in v0.4.0

func (a AsyncEvent) Registered(name string) bool

type Coro

type Coro struct {
	Func any
	Args Args
}

func C

func C(f any, args ...any) Coro

func NoArgsFunc

func NoArgsFunc(fs ...any) []Coro

func (Coro) Callback added in v0.5.0

func (c Coro) Callback(f func([]any)) *Task

func (Coro) ToTask

func (c Coro) ToTask() *Task

type Event added in v0.4.0

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

func (*Event) Abort added in v0.4.0

func (e *Event) Abort()

func (*Event) Cmd added in v0.4.0

func (e *Event) Cmd() string

func (*Event) Data added in v0.4.0

func (e *Event) Data() any

func (*Event) Get added in v0.4.0

func (e *Event) Get(name string) any

func (*Event) Set added in v0.4.0

func (e *Event) Set(name string, value any)

type H

type H []*Handle

func Await

func Await(coros ...Coro) H

func List

func List(args []Args, f any) H

func Map

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

func Slice

func Slice(args []Args, f any) H

func (H) Len

func (H H) Len() (sum int)

type Handle

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

func (*Handle) Done

func (h *Handle) Done() bool

func (*Handle) Len

func (h *Handle) Len() int

func (*Handle) Result

func (h *Handle) Result() []any

type Task

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

func (*Task) Bool added in v0.5.0

func (t *Task) Bool() bool

func (*Task) Error added in v0.5.0

func (t *Task) Error() error

Jump to

Keyboard shortcuts

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