Skip to main content

No project description provided

Project description

callback

给 Agent 的摘要:用 Callback 子类描述一次调用的载荷(字段);把具体逻辑用装饰器托管到注册函数里;trigger / atrigger等处理函数跑完再返回同一个实例,从上面读被更新后的状态。

定位

  • 把业务逻辑从调用点拆出去,由注册函数在触发时执行。
  • 同步 trigger:内部用线程池跑各注册函数并 future.result()阻塞直到全部结束。
  • 异步 atriggerasyncio.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

python_library_callback-0.1.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_library_callback-0.1.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file python_library_callback-0.1.3.tar.gz.

File metadata

  • Download URL: python_library_callback-0.1.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for python_library_callback-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3e6ed5257cc65f7869057e8a87c7b92296139cb0dc9837d538cb6392b99adf46
MD5 eafcb35d30f9b68d703138688f9d3b93
BLAKE2b-256 6d84ce86e37d3d00c0808522c368012c5643f918ddfa63d4763b594badb925ac

See more details on using hashes here.

File details

Details for the file python_library_callback-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for python_library_callback-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 85c7c9ef4b06ca8c3c92cb36344ac24c9bef863445160da431b638e6d424c690
MD5 cfe88e2feb852ed6c2f10ae3c1b57139
BLAKE2b-256 adb7ffd6f0b13b560a3fcfa8edea82e2e39bcce60704459dce47db206cbf6e43

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page