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.6.tar.gz (6.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.6-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_library_callback-0.1.6.tar.gz
  • Upload date:
  • Size: 6.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.6.tar.gz
Algorithm Hash digest
SHA256 20f0862294ae732b8a835eccab4c27ad80155090d6f8612fdac1d14a5806988d
MD5 d95ead42a3b9fd4d05e1125d853307fb
BLAKE2b-256 c0398ab194e3d57f1f5706f13aeebcf33522d0f1bebe6c437567ba362d59bbfe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_library_callback-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 84686c90c02235f10fcfd2f8af2cbe5de9dc517e0b558fc8051fbe2d1a8ae8a9
MD5 9f98f659459c27cd099788d9d00eb8ff
BLAKE2b-256 7d9861d149a95607ed13b5510cf4741a5f3cdff9575cab159b088313c6ebc7f2

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