Azure / Azure/azure-sdk-for-python

Add `is_empty()` method to `QueueClient`

Open
#44,631 3 comments 0 reactions 0 assignees View on GitHub
customer-reported needs-team-attention question
Dominant language
Python
Stars
5.6k
Forks
3.4k
Avg merge
1d 21h
Merged PRs (30d)
193

Description

**Is your feature request related to a problem? Please describe.**

I am frustrated by the lack of a straightforward, reliable way to check if a queue is truly empty. Currently, a common workaround is to attempt to receive a message with a short visibility timeout:

```python
def is_empty(self) -> bool:
return self.queue_client.receive_message(visibility_timeout=0) is None
```

However, this approach is flawed. It yields a false positive if the queue contains messages that are currently invisible (e.g., being processed by another worker). In this scenario, the method returns True, even though the queue is not technically empty and will have messages available again once visibility timeouts expire.

**Describe the solution you'd like**

I would like a built-in `is_empty()` method (or a similar property) on the QueueClient class. This method should ideally check the metadata of the queue to determine if there are any messages present, regardless of their visibility state.

**Additional context**

In a multi-consumer environment, the current "receive" hack causes logic errors where a system might shut down or trigger a "cleanup" task believing the work is done, while other consumers are still holding onto messages.

Below is the current wrapper implementation I am using, which highlights the need for a more robust built-in solution:

```python
class MyQueueClient():
def __init__(self, queue_name: str = "my-queue-test", connection_option: str = ""):
self.queue_service_client = (
self.__get_queue_service_from_connection_string(connection_option)
if self.__authenticate_fron_connection_str(connection_option)
else self.__get_queue_service_from_default_credential(connection_option)
)

try:
self.queue_client = self.queue_service_client.create_queue(queue_name)
except ResourceExistsError:
self.queue_client = self.queue_service_client.get_queue_client(queue_name)

# This is the problematic implementation I am trying to replace:
def is_empty(self) -> bool:
return self.queue_client.receive_message(visibility_timeout=0) is None
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.