Nico
Nico

Reputation: 782

Can't write message to SQS using boto3

I'm using boto3, AWS library to connect to their SQS services. I'm trying to connect, read and write messages from a queue but this doesn't seem to be working and the documentation isn't helping

here's my code, anyone can spot what I'm doing wrong?

#Connect to a session
session = Session(aws_access_key_id=SQSAccessKey, aws_secret_access_key=SQSSecretKey,region_name=sqsRegion)

#Connect to a resource
sqs= session.resource('sqs')

queue = sqs.get_queue_by_name(QueueName=transactionQueue)
print(queue.url)

# Create a new message
print 'creating new message'
toWrite = 'hello world'
response = queue.send_message(MessageBody=toWrite)
print(response.get('MessageId'))

#Reading messages in queue
messages = queue.receive_messages()
print 'there are %s messages in the queue' % len(messages)
for message in messages:
    # SQS Message
    message.body
    message.delete()

After sending the new message to the queue (and printing out the message ID) I try to read the queue for messages but it returns no new messages as if nothing was written to it.

Anything I'm doing wrong?

thanks!

Upvotes: 3

Views: 4338

Answers (2)

Jordon Phillips
Jordon Phillips

Reputation: 16003

You are not guaranteed to get any messages in response when the queue size is small. This is a property of distributed queues. See this page for more information.

Upvotes: 3

Nico
Nico

Reputation: 782

There were a couple of things at play here. For one thing, I was accidentally using a queue that was being read by another component (and deleting its messages). I was unaware of this. Also, Jordon's comment was also an issue since I was testing with one read at a time.

Upvotes: 2

Related Questions