Skip to content

Instantly share code, notes, and snippets.

@mz1991 mz1991/sqs_fifo.py
Last active Apr 6, 2017

Embed
What would you like to do?
AWS SQS FIFO
import boto3
import random
# This is for demo purpose, do not store your credentials in the code
AWS_ACCESS_KEY = 'yourAccessKey'
AWS_SECRET_ACCESS_KEY = 'yourSecretAccessKey'
sqs_client = boto3.resource(
'sqs',
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name='us-east-2' # this will work only with us-east-2 or us-west-2
)
queue_name = 'demo_queue.fifo'
response = sqs_client.create_queue(
QueueName=queue_name,
Attributes={
'FifoQueue': 'true',
'ContentBasedDeduplication' : 'true'
}
)
queue = sqs_client.get_queue_by_name(QueueName=queue_name)
# generate some sample numbers
samples = random.sample(range(1, 999999), 500)
# send the numbers to the queue
for i in samples:
queue.send_message(
MessageBody=str(i),
MessageGroupId='my_message_group_id'
)
received_messages = []
# consume the numbers
messages = queue.receive_messages()
while len(messages)>0:
for message in messages:
received_messages.append(int(message.body))
message.delete()
messages = queue.receive_messages()
# Check that the sent and consumed numbers are the same (also same order!)
print(samples == received_messages) # This will print True
import boto3
import random
# This is for demo purpose, do not store your credentials in the code
AWS_ACCESS_KEY = 'yourAccessKey'
AWS_SECRET_ACCESS_KEY = 'yourSecretAccessKey'
sqs_client = boto3.resource(
'sqs',
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name='us-east-1' # use any regions
)
queue_name = 'demo_queue'
response = sqs_client.create_queue(
QueueName=queue_name
)
queue = sqs_client.get_queue_by_name(QueueName=queue_name)
# generate some sample numbers
samples = random.sample(range(1, 999999), 500)
# send the numbers to the queue
for i in samples:
queue.send_message(
MessageBody=str(i),
MessageGroupId='my_message_group_id'
)
received_messages = []
# consume the numbers
messages = queue.receive_messages()
while len(messages)>0:
for message in messages:
received_messages.append(int(message.body))
message.delete()
messages = queue.receive_messages()
# Check that the sent and consumed numbers are the same (also same order!)
print(samples == fetched) # This will print False (the messages are not returned in the same order)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.