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

Function receive_messages

python/example_code/sqs/message_wrapper.py:101–126  ·  view source on GitHub ↗

Receive a batch of messages in a single request from an SQS queue. :param queue: The queue from which to receive messages. :param max_number: The maximum number of messages to receive. The actual number of messages received might be less. :param wait_time: Th

(queue, max_number, wait_time)

Source from the content-addressed store, hash-verified

99
100# snippet-start:[python.example_code.sqs.ReceiveMessage]
101def receive_messages(queue, max_number, wait_time):
102 """
103 Receive a batch of messages in a single request from an SQS queue.
104
105 :param queue: The queue from which to receive messages.
106 :param max_number: The maximum number of messages to receive. The actual number
107 of messages received might be less.
108 :param wait_time: The maximum time to wait (in seconds) before returning. When
109 this number is greater than zero, long polling is used. This
110 can result in reduced costs and fewer false empty responses.
111 :return: The list of Message objects received. These each contain the body
112 of the message and metadata and custom attributes.
113 """
114 try:
115 messages = queue.receive_messages(
116 MessageAttributeNames=["All"],
117 MaxNumberOfMessages=max_number,
118 WaitTimeSeconds=wait_time,
119 )
120 for msg in messages:
121 logger.info("Received message: %s: %s", msg.message_id, msg.body)
122 except ClientError as error:
123 logger.exception("Couldn't receive messages from queue: %s", queue)
124 raise error
125 else:
126 return messages
127
128
129# snippet-end:[python.example_code.sqs.ReceiveMessage]

Callers 1

usage_demoFunction · 0.85

Calls 1

receive_messagesMethod · 0.80

Tested by

no test coverage detected