MCPcopy Create free account
hub / github.com/awsdocs/aws-doc-sdk-examples / send_messages

Function send_messages

python/example_code/sqs/message_wrapper.py:53–94  ·  view source on GitHub ↗

Send a batch of messages in a single request to an SQS queue. This request may return overall success even when some messages were not sent. The caller must inspect the Successful and Failed lists in the response and resend any failed messages. :param queue: The queue to receiv

(queue, messages)

Source from the content-addressed store, hash-verified

51
52# snippet-start:[python.example_code.sqs.SendMessageBatch]
53def send_messages(queue, messages):
54 """
55 Send a batch of messages in a single request to an SQS queue.
56 This request may return overall success even when some messages were not sent.
57 The caller must inspect the Successful and Failed lists in the response and
58 resend any failed messages.
59
60 :param queue: The queue to receive the messages.
61 :param messages: The messages to send to the queue. These are simplified to
62 contain only the message body and attributes.
63 :return: The response from SQS that contains the list of successful and failed
64 messages.
65 """
66 try:
67 entries = [
68 {
69 "Id": str(ind),
70 "MessageBody": msg["body"],
71 "MessageAttributes": msg["attributes"],
72 }
73 for ind, msg in enumerate(messages)
74 ]
75 response = queue.send_messages(Entries=entries)
76 if "Successful" in response:
77 for msg_meta in response["Successful"]:
78 logger.info(
79 "Message sent: %s: %s",
80 msg_meta["MessageId"],
81 messages[int(msg_meta["Id"])]["body"],
82 )
83 if "Failed" in response:
84 for msg_meta in response["Failed"]:
85 logger.warning(
86 "Failed to send: %s: %s",
87 msg_meta["MessageId"],
88 messages[int(msg_meta["Id"])]["body"],
89 )
90 except ClientError as error:
91 logger.exception("Send messages failed to queue: %s", queue)
92 raise error
93 else:
94 return response
95
96
97# snippet-end:[python.example_code.sqs.SendMessageBatch]

Callers 1

usage_demoFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected