A high-performance, simple-structured event system, relies on asyncio
Project description
Letoderea
一个高性能,结构简洁,依赖于 Python内置库asyncio 的事件系统, 设计灵感来自Graia BroadcastControl。
项目仍处于开发阶段,部分内容可能会有较大改变
安装
从 PyPI 安装
pip install arclet-letoderea
样例
基本使用
import asyncio
from arclet.letoderea import es, make_event
@make_event
class TestEvent:
name = "Letoderea"
@es.on()
async def test_subscriber(name: str):
print(name)
async def main():
await es.publish(TestEvent())
asyncio.run(main())
依赖注入
import asyncio
from arclet.letoderea import es, make_event, Depends
@make_event
class TestEvent:
name = "Letoderea"
async def get_msg(event):
return f"Hello, {event.name}"
@es.on(TestEvent)
async def test_subscriber(msg: str = Depends(get_msg)):
print(msg)
async def main():
await es.publish(TestEvent())
asyncio.run(main())
通信
import asyncio
import random
from dataclasses import dataclass
from arclet.letoderea import es, make_event
@make_event
@dataclass
class Event:
name: str
@dataclass
class RandomData:
seed: int
__result_type__ = float
@es.define("rand", RandomData).register()
def random_subscriber(seed: int):
return random.Random(seed).random()
@es.on(Event)
async def event_subscriber(event: Event):
print(f"Event: {event.name}")
result = await es.post(RandomData(42))
if result:
print(f"Random: {result.value}")
async def main():
await es.publish(Event("Letoderea"))
asyncio.run(main())
说明
事件
- 事件可以是任何对象,只要实现了
gather异步方法, 或使用EventSystem.define并传入supplier参数 gather方法的参数为Contexts类型,用于传递上下文信息- 事件可以通过
gather方法将自身想要传递的信息整合进Contexts中 - 事件可以携带
Provider与Auxiliary,它们会在事件被订阅时注入到订阅者中 - 订阅子类事件时,父类事件的
Provider与Auxiliary会被继承 - 订阅父类事件时,其子类事件也会被分发给订阅者
订阅
- 通过
Publisher.register,EventSystem.on,EventSystem.use或subscribe装饰器可以将一个函数注册为事件的订阅者 - 上述方法会返回
Subscriber类型对象,可以通过其.dispose方法取消订阅 - 订阅者的参数可以是任何类型,事件系统会尝试从
Contexts中查找对应的值并注入 - 默认情况下
event为名字的参数会被注入为事件的实例 - 订阅者可以设置优先级,值越小优先级越高
上下文
Contexts类型是一个dict的子类,用于传递上下文信息,除此之外与dict没有区别Contexts默认包含$event键,其值为事件的实例Contexts默认包含$subscriber键,其值为订阅者的实例- 在订阅者的函数执行后,其结果会被存储在
Contexts中,键为$result - 若在解析参数时抛出异常,异常值会被存储在
Contexts中,键为$error
依赖注入
Provider[T]负责管理参数的注入, 其会尝试从Contexts中选择需求的参数返回- 对于订阅者的每个参数,在订阅者注册后,事件系统会遍历该订阅者拥有的所有
Provider, 并依次调用Provider.validate方法,如果返回True,则将该Provider绑定到该参数上。 当进行依赖解析时,事件系统会遍历该参数绑定的所有Provider,并依次调用Provider.__call__方法, 如果返回值不为None,则将该返回值注入到该参数中。 Provider.validate方法用于验证订阅函数的参数是否为该Provider可绑定的参数。默认实现为检查目标参数的类型声明是否为T。 也可以通过重写该方法来实现自定义的验证逻辑。Provider.__call__方法用于从Contexts中获取参数- 原则上
Provider只负责注入单一类型的参数。若想处理多个类型的参数,可以声明自己为Provider[Union[A, B, ...]]类型, 并在Provider.validate方法中进行自定义的逻辑判断。但更推荐的做法是构造多个Provider,并将其绑定到同一个参数上。 - 对于特殊的辅助器
Depend,事件系统会将其作为特殊的Provider处理,绑定了Depend的参数在解析时将直接调用设置在Depend上的方法。 Provider可以设置优先级,值越小优先级越高- 另有
ProviderFactory,用于集成多个Provider的分配,以方便event.providers的设置
事件发布
- 一般情况下通过
EventSystem.publish或EventSystem.post方法可以发布一个事件让事件系统进行处理 publish会处理所有合适的订阅者,而post会在某一个订阅者返回了有效值后停止处理,并返回该值Publisher类负责管理订阅者与事件的交互Publisher.validate方法用于验证该事件是否为该发布者的订阅者所关注的事件Publisher.emit和Publisher.bail方法用于将事件直接分发给属于自身的订阅者,emit与bail的区别类似于publish与postPublisher.supply方法用于让事件系统主动获取事件并分发给所有订阅者EventSystem.use,EventSystem.publish和EventSystem.post可以指定Publisher- 通过
EventSystem.define可以便捷的定义发布者,并在.use等处通过定义的名字引用
辅助
Auxiliary提供了一系列辅助方法,方便事件的处理Auxiliary分为Judge,Supply两类:Judge: 用于判断此时是否应该处理事件Supply: 用于为Contexts提供额外的信息
Auxiliary.scopes声明了Auxiliary的作用域:prepare: 表示该Auxiliary会在依赖注入之前执行complete: 表示该Auxiliary会在依赖注入完成后执行onerror: 表示该Auxiliary会在上述两个作用域中发生异常时执行cleanup: 表示该Auxiliary会在事件处理完成后执行
Auxiliary可以设置优先级,值越小优先级越高Auxiliary的__call__方法多增加了一个Interface参数,可用于操作Contexts或Provider, 获取已执行的Auxiliary等
开源协议
本实现以 MIT 为开源协议。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
arclet_letoderea-0.12.2.tar.gz
(24.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file arclet_letoderea-0.12.2.tar.gz.
File metadata
- Download URL: arclet_letoderea-0.12.2.tar.gz
- Upload date:
- Size: 24.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.19.1 CPython/3.9.13 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7347b60bbdb913c65f5ea5282cb23f4c54793e2c1e66dccec5f58c1125732159
|
|
| MD5 |
e1a58e161555967e82d1f556edbb3bfd
|
|
| BLAKE2b-256 |
1e254ecf46f98244d82fef472a840c39b6c0b54152000e307b9dc1c4b45900bc
|
File details
Details for the file arclet_letoderea-0.12.2-py3-none-any.whl.
File metadata
- Download URL: arclet_letoderea-0.12.2-py3-none-any.whl
- Upload date:
- Size: 22.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.19.1 CPython/3.9.13 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc8adaf16dba2c25037d40ae1cc9f5f94cbd43a7cf41669bd815a8450f66c33b
|
|
| MD5 |
243d9dab7a668d4f4b3d1a8aa2b9c839
|
|
| BLAKE2b-256 |
ddb8efbd4a88adc90d70e1908f640c0b05dc397cb88074ed6c697f7a3fba914d
|