Sreejithc321
Sreejithc321

Reputation: 307

Efficient method to read messages from sqs without polling consecutively

I am very new to AWS SQS queues and I am currently playing around with python and boto. Now I am able to read messages from SQS by polling consecutively.

The script is as follows:

while 1:
    m = q.read(wait_time_seconds=10)
    if m:
        print m

How do I make this script constantly listen for new additions to the queue without using while loop? Is there a way to write a Python consumer for SQS that doesn't have to poll periodically for new messages?

Upvotes: 2

Views: 2202

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 178956

Not really... that's how SQS works. If a message arrives during the wait, it will be returned almost immediately.

This is not the inefficient operation that it seems like.

If you increase your timeout to the max allowed 20 seconds, then, worst case, you will generate no more than about 3 x 60 x 24 x 30 = 129,600 "empty" polls per month... × $0.00000050 per poll = $0.0648. (The first 1,000,000 requests are billed at $0.)

Note that during the timeout, if a new message arrives, it will return almost immediately, not wait the full 20 sec.

Upvotes: 6

Related Questions