跳转到主要内容

一个简单的后端无关心跳协议

项目描述

beatit

一个简单的后端无关心跳协议。

>>> class Printer:
...     @staticmethod
...     def publish(*, subject: bytes, payload: bytes):
...         print(f"{payload} -> {subject}")
>>> from beatit import Heart
>>> heart = Heart(process="my.process.identifier", publisher=Printer)
>>> heart.start(warmup=60)
b'start/60' -> b'heartbeat.my.process.identifier'
>>> heart.beat(period=5)
b'beat/5' -> b'heartbeat.my.process.identifier'
>>> heart.beat(period=5)
>>> heart.degraded(period=5)
b'degraded/5' -> b'heartbeat.my.process.identifier'
>>> heart.degraded(period=5)
>>> heart.stop()
b'stop' -> b'heartbeat.my.process.identifier'

您需要的只是一个具有接受两个命名参数(subject和payload)的publish方法的发布者。

使用进程名称和发布者实例化一个Heart实例。 beats 将在subject heartbeat.<process>上发布。

什么是beats

start带有预热期(之后预期一个beatdegraded

beat带有period(何时预期下一个beat)。

degraded带有period(何时预期下一个beatdegraded)。

stop当进程优雅地停止时。

同步或不...

如果您喜欢异步(你有什么问题?)Heart会自动识别异步发布者,您只需等待所有操作即可。

>>> import asyncio
>>> class AsyncPrinter:
...     @staticmethod
...     async def publish(*, subject: bytes, payload: bytes):
...         print(f"{payload} -> {subject}")
>>> from beatit import Heart
>>> heart = Heart(process="my.process.identifier", publisher=AsyncPrinter)
>>> asyncio.run(heart.start(warmup=60))
b'start/60' -> b'heartbeat.my.process.identifier'
>>> asyncio.run(heart.beat(period=5))
b'beat/5' -> b'heartbeat.my.process.identifier'
>>> asyncio.run(heart.beat(period=5))
>>> asyncio.run(heart.degraded(period=5))
b'degraded/5' -> b'heartbeat.my.process.identifier'
>>> asyncio.run(heart.degraded(period=5))
>>> asyncio.run(heart.stop())
b'stop' -> b'heartbeat.my.process.identifier'

subject_as_string

beatit使用bytes类型的subject和payload调用publish。看起来很一致,但常见的发布者更喜欢subject为str。

不用担心,这里有一个@limx0批准的解决方案!

>>> class StrBytes:
...     @staticmethod
...     def publish(*, subject: str, payload: bytes):
...         print(f"{payload} -> {subject}")
>>> from beatit import Heart
>>> heart = Heart(process="my.process.identifier", publisher=StrBytes, subject_as_string=True)
>>> heart.start(warmup=30)
b'start/30' -> heartbeat.my.process.identifier

项目详情


下载文件

下载适用于您平台文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

beatit-2022.3.1.tar.gz (4.0 kB 查看哈希值)

上传于 来源

构建发行版

beatit-2022.3.1-py2.py3-none-any.whl (3.5 kB 查看哈希值)

上传于 Python 2 Python 3

由以下支持