Sending Messages¶
Send a Single Message¶
Send with Headers¶
Headers are optional metadata dictionaries attached to the message:
msg_id = queue.send(
"my_queue",
{"task": "process_payment"},
headers={"priority": "high", "source": "webhook"},
)
Send with Delay¶
Delay message delivery by an integer number of seconds or a specific datetime:
# Delay by 60 seconds
msg_id = queue.send("my_queue", {"task": "reminder"}, delay=60)
# Delay until a specific time
from datetime import datetime, timezone
future = datetime.now(timezone.utc).replace(hour=23, minute=0)
msg_id = queue.send("my_queue", {"task": "nightly_report"}, delay=future)
Backward Compatible Alias¶
The tz parameter is an alias for delay and exists for backward compatibility:
Send a Batch¶
Send multiple messages in one transaction for higher throughput:
messages = [
{"task": "email_user_1"},
{"task": "email_user_2"},
{"task": "email_user_3"},
]
msg_ids: list[int] = queue.send_batch("my_queue", messages)
Batch with Headers¶
headers = [
{"priority": "high"},
{"priority": "low"},
{"priority": "high"},
]
msg_ids = queue.send_batch("my_queue", messages, headers=headers)
The
headerslist must be the same length asmessages.
Batch with Delay¶
Return Value¶
sendreturns a singleint(message ID).send_batchreturnslist[int].
On failure or unsupported configurations, -1 or an empty list may be returned.