Ack All
Acknowledge all received KubeMQ queue messages at once using the Python SDK poll API.
Overview
ack_all_queue_messages acknowledges every pending message on a channel in a single broker-side call, without receiving them first. Reach for it when you want to drain a queue rather than process it — clearing a backlog of stale work after a bad deploy, resetting a channel between test runs, or discarding messages that are no longer relevant — where pulling and acking each message individually would be slow and wasteful.
Because it settles the whole channel at once, it is far cheaper than a receive-then-ack loop: the broker confirms all in-flight messages atomically and reports how many were affected via wait_time_seconds, which bounds how long it waits for in-flight transactions to settle before counting.
Gotchas: this is a blunt, irreversible instrument — it acknowledges all currently-pending messages, not a selected subset, so anything unprocessed is discarded, not redelivered. A busy channel may need a larger wait_time_seconds value to catch messages still landing. For routine, per-message cleanup use ordinary acks, an expiration policy, or a dead-letter policy instead — save ack-all for deliberate, wholesale purges.
Prerequisites
- KubeMQ server running on
localhost:50000 - Python SDK installed (
pip install kubemq)
Code
"""Example: Ack all — acknowledge all pending messages in a queue at once."""
from __future__ import annotations
import asyncio
from kubemq import AsyncQueuesClient
from kubemq import QueueMessage
async def main() -> None:
async with AsyncQueuesClient(
address="localhost:50000",
client_id="python-queues-ack-all-client",
) as client:
# Send multiple messages
for i in range(10):
await client.send_queue_message(
QueueMessage(
channel="python-queues.ack-all",
body=f"Msg-{i + 1}".encode(),
)
)
print("Sent 10 messages")
# Acknowledge all pending messages at once
acked = await client.ack_all_queue_messages(
"python-queues.ack-all", wait_time_seconds=5
)
print(f"Acknowledged {acked} messages")
if __name__ == "__main__":
asyncio.run(main())
How It Works
ack_all_queue_messagessends a single bulk-acknowledge command to the broker, atomically acknowledging all pending messages in the channel.- The
wait_time_secondsargument controls how long the broker waits for in-flight messages to be included before executing the bulk ack. - The return value is the count of messages that were acknowledged; it can be zero if no messages were pending.
- Bulk ack is more efficient than calling
async_ack()per message when you need to drain a queue unconditionally.
Related
Was this page helpful?