【发布时间】:2020-11-18 18:58:09
【问题描述】:
我正在尝试使用 async/await 从 Azure ServiceBus 主题读取消息,然后通过 HTTP 将内容转发到另一个应用程序。我的代码很简单:
import asyncio
from aiohttp import ClientSession
from azure.servicebus.aio.async_client import ServiceBusService
bus_service = ServiceBusService(service_namespace=..., shared_access_key_name=..., shared_access_key_value=...)
async def watch(topic_name, subscription_name):
print('{} started'.format(topic_name))
message = bus_service.receive_subscription_message(topic_name, subscription_name, peek_lock=False, timeout=1)
if message.body is not None:
async with ClientSession() as session:
await session.post('ip:port/endpoint',
headers={'Content-type': 'application/x-www-form-urlencoded'},
data={'data': message.body.decode()})
async def do():
while True:
for topic in ['topic1', 'topic2', 'topic3']:
await watch(topic, 'watcher')
if __name__ == "__main__":
asyncio.run(do())
我想(永远)查找来自各种主题的消息,并在消息到达时发送 POST。我从azure 导入了aio 包,它应该以异步方式工作。经过多次尝试,我得到的唯一解决方案是使用while True 并设置timeout=1。这不是我想要的,我是按顺序做的。
azure-servicebus 版本0.50.3。
这是我第一次使用 async/await 可能我错过了什么......
有什么解决方案/建议吗?
【问题讨论】:
-
请使用 azure-servicebus 7.0.0 使用 asyncio pypi.org/project/azure-servicebus
标签: python python-3.x async-await azureservicebus azure-servicebus-topics