I'm trying to read messages from Azure ServiceBus Topics using async/await
and then forward the content to another application via HTTP. My code is simple:
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())
I want to look for messages (forever) from various topics and when a message arrives send the POST. I import the aio
package from azure
which should work in an async way. After many attempts, the only solution I got is this with while True
and setting the timeout=1
. This is not what I wanted, I'm doing it sequentially.
azure-servicebus
version 0.50.3
.
This is my first time with async/await
probably I'm missing something...
Any solution/suggestions?