You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何关闭Azure Service Bus队列的连接?(附消息接收代码示例)

Question

I've successfully been able to receive messages from an Azure Queue using the official Azure SDK, and everything runs fine. Here's the code snippet I'm using to receive messages:

from azure.servicebus import QueueClient
client = QueueClient.from_connection_string(q_string, q_name)
msg = None
with client.get_receiver() as queue_receiver:
    messages = queue_receiver.fetch_next(max_batch_size=1, timeout=3)
    if len(messages) > 0:
        msg = messages[0]
        print(f"Received {msg.message}")
return msg

How can I close the connection to this queue? Are there any relevant functions available in the Azure SDK?


Answer

Great to hear your message receiving code is working smoothly! Let’s break down how to properly close the queue connection based on the SDK version you’re using:

For the legacy azure-servicebus SDK (your current setup)

You’re using the older QueueClient class, and you have two straightforward options to clean up the connection:

  1. Explicitly call the close() method
    Once you’re done processing messages, you can manually call client.close() to shut down the underlying connection. Using a finally block ensures this runs even if an error occurs during processing:
from azure.servicebus import QueueClient
client = QueueClient.from_connection_string(q_string, q_name)
msg = None
try:
    with client.get_receiver() as queue_receiver:
        messages = queue_receiver.fetch_next(max_batch_size=1, timeout=3)
        if len(messages) > 0:
            msg = messages[0]
            print(f"Received {msg.message}")
finally:
    # Close the client connection
    client.close()
return msg
  1. Use QueueClient as a context manager
    Wrap the QueueClient itself in a with statement—this will automatically close the connection when the block exits, no manual close() call needed. This is the cleaner, more Pythonic approach:
from azure.servicebus import QueueClient
msg = None
with QueueClient.from_connection_string(q_string, q_name) as client:
    with client.get_receiver() as queue_receiver:
        messages = queue_receiver.fetch_next(max_batch_size=1, timeout=3)
        if len(messages) > 0:
            msg = messages[0]
            print(f"Received {msg.message}")
return msg

Note: Your existing code already manages the receiver connection via the with block, but the QueueClient connection stays open unless you handle it with one of the above methods.

Quick note on the latest SDK (v7+)

If you ever upgrade to the modern azure-servicebus SDK, the API uses ServiceBusClient instead of QueueClient. The same context manager pattern applies here, and it’s the recommended way to handle connections:

from azure.servicebus import ServiceBusClient

msg = None
with ServiceBusClient.from_connection_string(q_string) as client:
    with client.get_queue_receiver(q_name) as receiver:
        messages = receiver.receive_messages(max_message_count=1, max_wait_time=3)
        for message in messages:
            msg = message
            print(f"Received {message.body}")
            receiver.complete_message(message)
return msg

Here, the context managers handle closing both the client and receiver connections automatically.

内容的提问来源于stack exchange,提问作者Amanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:42:33