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

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_library_callback-0.1.4.tar.gz
  • Upload date:
  • Size: 5.2 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.4.tar.gz
Algorithm Hash digest
SHA256 9fa3736ba6475c63f7bb8518ff520a501602dad22d3a9196d79271d246ddc1af
MD5 3bdecf0df056e0c1ae6e6a717fb81e3f
BLAKE2b-256 04b140caa734b0691d46328d8a12677877f3733021c9c80880149ad397e44281

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_library_callback-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2a658769ceffb3f08ebe8cf42b81dfe45cbebaff3090d3730bb038b5c8021263
MD5 a17c4168fa1105a246decf39f00a8cee
BLAKE2b-256 02f706c6033b5a949a01d509e203681168288e335b760db4beb6fa1454f672fd

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