小编Bil*_*ill的帖子

如何在 Python 中订阅 NATS 主题并持续接收消息?

我尝试了下面的示例(来自页面):

nc = NATS()

await nc.connect(servers=["nats://demo.nats.io:4222"])

future = asyncio.Future()

async def cb(msg):
  nonlocal future
  future.set_result(msg)

await nc.subscribe("updates", cb=cb)
await nc.publish("updates", b'All is Well')
await nc.flush()

# Wait for message to come in
msg = await asyncio.wait_for(future, 1)
Run Code Online (Sandbox Code Playgroud)

但这似乎只对接收一条消息有用。我如何订阅并继续接收消息?

我也看过包 example,但它似乎只是玩双方的对话,然后退出。

python python-asyncio nats.io

2
推荐指数
1
解决办法
8234
查看次数

标签 统计

nats.io ×1

python ×1

python-asyncio ×1