Python - Receive from Azure Service Bus Topic is very slow

1.6k views Asked by At

I am toying around with home automation, and I am planning to use Azure Service Bus as my "core" for message handling. With the .NET SDKs everything works perfectly and is fast enough (milliseconds for send + receive). However, I am now using the "azure.servicebus" module with Python (Debian on a Raspberry Pi), and the receive_subscription_message call is far from fast. It varies between near instant to lagging a minute behind.

My code is as follows:

from azure.servicebus import ServiceBusService, Message, Queue
bus_service = ServiceBusService(
    service_namespace='mynamespace',
    shared_access_key_name='Listener1',
    shared_access_key_value='...')
msg = bus_service.receive_subscription_message('messages', 'ListenerTest.py', peek_lock=True)
msg.delete()

I have toyed around with peek_lock True and False, but the behaviour is the same.

Has anyone else been able to get this stable / near instant?

1

There are 1 answers

0
Ming Xu - MSFT On

Please make sure there has indeed messages in the subscription, also please be aware that .NET SDK by default uses a Service Bus specific protocol instead of http, but the Python SDK uses http polling (basically check if there're messages in the subscription once in a while). We can find the brief info at https://github.com/Azure/azure-sdk-for-python/blob/master/doc/servicebus.rst:

ServiceBus Queues are an alternative to Storage Queues that might be useful in scenarios where more advanced messaging features are needed (larger message sizes, message ordering, single-operaiton destructive reads, scheduled delivery) using push-style delivery (using long polling).

Per my understanding this might explain why you see the message received either instantly or up to a minute. Based on behavior that you described, you might want to use AMQP, which is based on bi-directional TCP, and thus does not require polling. To use AMQP, you may want to leverage the standard Proton-Python library, I'd like to suggest you to check https://msdn.microsoft.com/en-us/library/azure/jj841070.aspx for a sample. But please note the tips from that article:

Note that at the time of this writing, the SSL support in Proton-C is only available for Linux operating systems. Because Microsoft Azure Service Bus requires the use of SSL, Proton-C (and the language bindings) can only be used to access Microsoft Azure Service Bus from Linux at this time. Work to enable Proton-C with SSL on Windows is underway so check back frequently for updates.