No project description provided
Project description
callback
给 Agent 的摘要:用 Callback 子类描述一次调用的载荷(字段);把具体逻辑用装饰器托管到注册函数里;trigger / atrigger 会等处理函数跑完再返回同一个实例,从上面读被更新后的状态。
定位
- 把业务逻辑从调用点拆出去,由注册函数在触发时执行。
- 同步
trigger:内部用线程池跑各注册函数并future.result(),阻塞直到全部结束。 - 异步
atrigger:asyncio.gather等待全部协程,需在 async 上下文里await。 - 返回值始终是本次触发的
Callback实例;惯例是在 handler 里改实例上的字段作为结果。
最小用法
from callback import Callback
class OrderPaid(Callback):
order_id: str
amount: int # handler 里可改写
@OrderPaid
def on_paid(cb: OrderPaid) -> None:
cb.amount += 1 # 示例:更新状态
cb = OrderPaid.trigger(order_id="a", amount=100)
# cb 即本次载荷;阻塞已结束,可读更新后的字段
assert cb.amount == 101
异步回调:子类设 _async = True,用 @Cls 注册 async def,用 await Cls.atrigger(...)。
规则(实现相关)
- 载荷字段:类体里类型注解的普通属性;
ClassVar、以下划线_开头的名字不算字段。 - 注册:
@MyCallback装饰函数;可def h(cb: MyCallback)或def h()(无参)。同一函数对象若再次装饰注册,只会保留一份,触发时该函数不会执行两次(两个不同的函数会各执行一次)。 - 同步 / 异步必须一致:同步类不能注册
async def;异步类(_async = True)不能注册普通def。 - 可重写钩子:
before_trigger/after_trigger(同步),before_atrigger/after_atrigger(异步)。 - 无注册函数时,
trigger/atrigger仍会构造并返回实例。
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 python_library_callback-0.1.5.tar.gz.
File metadata
- Download URL: python_library_callback-0.1.5.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
948c48793690e5dac7ece1a3562c83a4a6538ea4e43c171f0e63095a895d736d
|
|
| MD5 |
780d0a7b15f6a34f27b7f5471158e582
|
|
| BLAKE2b-256 |
ba4aec00f2475fd1fdcc5dc64515e7bd5e4e5f5513e11103e7ef8a0cd11a0bad
|
File details
Details for the file python_library_callback-0.1.5-py3-none-any.whl.
File metadata
- Download URL: python_library_callback-0.1.5-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9185ed0c711b9ef93eed3f71925fb6d8cca2b0ab1ffc176b0f9022c02be3191
|
|
| MD5 |
660c47659d59f15d62d0b8b57dd2d53a
|
|
| BLAKE2b-256 |
7a87c60369b361644abc18f63338f6f4e69c87f45ce73ed196fd2ed05599cec3
|