Skip to main content
 首页 » 编程设计

Python之从 Azure 服务总线主题接收非常慢

2024年10月24日21lori

我正在研究家庭自动化,我计划使用 Azure 服务总线作为我的消息处理“核心”。使用 .NET SDK,一切都可以完美运行并且速度足够快(发送 + 接收只需几毫秒)。但是,我现在正在将“azure.servicebus”模块与 Python(Raspberry Pi 上的 Debian)一起使用,并且 receive_subscription_message 调用远非快速。它在接近瞬间到滞后一分钟之间变化。

我的代码如下:

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() 

我试过 peek_lock True 和 False,但行为是一样的。

有没有其他人能够获得这种稳定/近乎即时的?

请您参考如下方法:

请确保订阅中确实有消息,另外请注意.NET SDK 默认使用服务总线特定协议(protocol)而不是 http,但 Python SDK 使用 http 轮询(基本上检查是否有消息订阅一次)。我们可以在https://github.com/Azure/azure-sdk-for-python/blob/master/doc/servicebus.rst找到简要信息。 :

ServiceBus 队列是存储队列的替代方案,在需要更高级的消息传递功能(更大的消息大小、消息排序、单操作破坏性读取、计划交付)使用推送的情况下可能很有用-样式传递(使用长轮询)。

根据我的理解,这可以解释为什么您会立即或最多一分钟看到收到的消息。根据您描述的行为,您可能希望使用基于双向 TCP 的 AMQP,因此不需要轮询。要使用 AMQP,您可能需要利用标准的 Proton-Python 库,我建议您查看 https://msdn.microsoft.com/en-us/library/azure/jj841070.aspx一个 sample 。但请注意该文章中的提示:

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.