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

Function usage_demo

python/example_code/sqs/message_wrapper.py:187–259  ·  view source on GitHub ↗

Shows how to: * Read the lines from this Python file and send the lines in batches of 10 as messages to a queue. * Receive the messages in batches until the queue is empty. * Reassemble the lines of the file and verify they match the original file.

()

Source from the content-addressed store, hash-verified

185
186# snippet-start:[python.example_code.sqs.Scenario_SendReceiveBatch]
187def usage_demo():
188 """
189 Shows how to:
190 * Read the lines from this Python file and send the lines in
191 batches of 10 as messages to a queue.
192 * Receive the messages in batches until the queue is empty.
193 * Reassemble the lines of the file and verify they match the original file.
194 """
195
196 def pack_message(msg_path, msg_body, msg_line):
197 return {
198 "body": msg_body,
199 "attributes": {
200 "path": {"StringValue": msg_path, "DataType": "String"},
201 "line": {"StringValue": str(msg_line), "DataType": "String"},
202 },
203 }
204
205 def unpack_message(msg):
206 return (
207 msg.message_attributes["path"]["StringValue"],
208 msg.body,
209 int(msg.message_attributes["line"]["StringValue"]),
210 )
211
212 print("-" * 88)
213 print("Welcome to the Amazon Simple Queue Service (Amazon SQS) demo!")
214 print("-" * 88)
215
216 queue = queue_wrapper.create_queue("sqs-usage-demo-message-wrapper")
217
218 with open(__file__) as file:
219 lines = file.readlines()
220
221 line = 0
222 batch_size = 10
223 received_lines = [None] * len(lines)
224 print(f"Sending file lines in batches of {batch_size} as messages.")
225 while line < len(lines):
226 messages = [
227 pack_message(__file__, lines[index], index)
228 for index in range(line, min(line + batch_size, len(lines)))
229 ]
230 line = line + batch_size
231 send_messages(queue, messages)
232 print(".", end="")
233 sys.stdout.flush()
234 print(f"Done. Sent {len(lines) - 1} messages.")
235
236 print(f"Receiving, handling, and deleting messages in batches of {batch_size}.")
237 more_messages = True
238 while more_messages:
239 received_messages = receive_messages(queue, batch_size, 2)
240 print(".", end="")
241 sys.stdout.flush()
242 for message in received_messages:
243 path, body, line = unpack_message(message)
244 received_lines[line] = body

Callers 1

message_wrapper.pyFile · 0.70

Calls 7

pack_messageFunction · 0.85
send_messagesFunction · 0.85
receive_messagesFunction · 0.85
unpack_messageFunction · 0.85
delete_messagesFunction · 0.85
create_queueMethod · 0.80
deleteMethod · 0.45

Tested by

no test coverage detected