Azure/azure-storage-python

QueueClient.receive_messages() receives more than one message

markkuleinio opened this issue · 2 comments

Which service(blob, file, queue) does this issue concern?

Queue

Which version of the SDK was used? Please provide the output of pip freeze.

azure-storage-queue==12.1.1
azure-core==1.5.0

What problem was encountered?

QueueClient.receive_messages(messages_per_page=1) returns more than one message from the queue.

help(QueueClient.receive_messages) says:

Help on function receive_messages in module azure.storage.queue._queue_client:

receive_messages(self, **kwargs)
    Removes one or more messages from the front of the queue.
...
    :keyword int messages_per_page:
        A nonzero integer value that specifies the number of
        messages to retrieve from the queue, up to a maximum of 32. If
        fewer are visible, the visible messages are returned. By default,
        a single message is retrieved from the queue with this operation.

Testing code:

>>> from azure.storage.queue import QueueClient
>>> q = QueueClient(account_url="https://the_account_that_is_used.queue.core.windows.net", credential="xxx", queue_name="receive-test-2020-06-02", connection_timeout=10)
>>> q.get_queue_properties()
{'name': 'receive-test-2020-06-02', 'metadata': {}, 'approximate_message_count': 0}
>>> q.send_message("test1")
{'id': '28dd2ab0-ccdc-4f8e-a8a1-be6a8f980b21', 'inserted_on': datetime.datetime(2020, 6, 2, 6, 43, 9, tzinfo=datetime.timezone.utc), 'expires_on': datetime.datetime(2020, 6, 9, 6, 43, 9, tzinfo=datetime.timezone.utc), 'dequeue_count': None, 'content': 'test1', 'pop_receipt': 'AgAAAAMAAAAAAAAAkutnFKk41gE=', 'next_visible_on': datetime.datetime(2020, 6, 2, 6, 43, 9, tzinfo=datetime.timezone.utc)}
>>> q.send_message("test2")
{'id': '5944194a-1643-4614-9a5b-102cc2466544', 'inserted_on': datetime.datetime(2020, 6, 2, 6, 43, 17, tzinfo=datetime.timezone.utc), 'expires_on': datetime.datetime(2020, 6, 9, 6, 43, 17, tzinfo=datetime.timezone.utc), 'dequeue_count': None, 'content': 'test2', 'pop_receipt': 'AgAAAAMAAAAAAAAA2TX4GKk41gE=', 'next_visible_on': datetime.datetime(2020, 6, 2, 6, 43, 17, tzinfo=datetime.timezone.utc)}
>>> q.send_message("test3")
{'id': 'd61739d4-39f8-463c-b7f2-d71d5e8b1c2a', 'inserted_on': datetime.datetime(2020, 6, 2, 6, 43, 20, tzinfo=datetime.timezone.utc), 'expires_on': datetime.datetime(2020, 6, 9, 6, 43, 20, tzinfo=datetime.timezone.utc), 'dequeue_count': None, 'content': 'test3', 'pop_receipt': 'AgAAAAMAAAAAAAAAGeKTGqk41gE=', 'next_visible_on': datetime.datetime(2020, 6, 2, 6, 43, 20, tzinfo=datetime.timezone.utc)}
>>>
>>> msgs = q.receive_messages(visibility_timeout=120, messages_per_page=1)
>>> for msg_batch in msgs.by_page():
...     for msg in msg_batch:
...             print(msg.content)
...
test1
test2
test3
>>>

As you can see, all three messages were returned with the single call, even though messages_per_page is 1.

How do I get only one message from the queue?

One problem in this current state is the visibility_timeout: When there are lots of messages in the queue, receive_messages() gets them all, and the handling of each message takes a long time, it is hard to tune the visibility_timeout to a useful value.

Have you found a mitigation/solution?

No

Also note that apparently the documentation is currently in https://docs.microsoft.com/en-us/python/api/azure-storage-queue/azure.storage.queue.queueclient?view=azure-python#receive-messages---kwargs- and that does not describe the parameters at all.

This issue is tracked in another repo #665. so we will close this one