Skip to main content

py-sqs-queue

Simple Python AWS SQS queue consumer and publisher

Installation

python setup.py install

Examples

from sqs_queue import Queue

my_queue = Queue('YOUR_QUEUE_NAME')
for message in my_queue:
    your_process_fn(message)

Or, if you'd like to leave unprocessable messages in the queue to be retried again later:

for message in my_queue:
    try:
        your_process_fn(message)
    except YourRetryableError:
        message.defer()
    except Exception as e:
        logger.warn(e)

And, you can publish to the queue as well:

queue.publish({'MessageId': 123, 'Message': '{"foo": "bar"}'})

If you already have a boto3 queue resource, pass this instead of a name:

import boto3
from sqs_queue import Queue

queue_resource = boto3.resource('sqs').Queue('YOUR_QUEUE_NAME')

my_queue = Queue(queue=queue_resource)

Configuration

You can put your AWS credentials in environment variables or any of the other places boto3 looks.

Other parameters can be passed into the Queue() initiator, or set with environment variables prefixed by SQS_QUEUE_, e.g. SQS_QUEUE_POLL_WAIT.

Parameters

poll_wait and poll_sleep

Behind the scenes, the generator is polling SQS for new messages. When the queue is empty, that call will wait up to 20 seconds for new messages, and if it times out before any arrive it will sleep for 40 seconds before trying again. Those time intervals are configurable:

queue = Queue('YOUR_QUEUE_NAME', poll_wait=20, poll_sleep=40)

drain

Normally, once the queue is empty, the generator waits for more messages. If you just want to process all existing messages and quit, you can pass this boolean parameter:

queue = Queue('YOUR_QUEUE_NAME', drain=True)

For example, if your queue is long and your consumers are falling behind, you can start a bunch of consumers with drain=True and they'll quit when you've caught up.

sns

If your SQS queue is being fed from an SNS topic, you can pass your Queue this boolean parameter, and then your messages will just contain the SNS notification data, so you don't have to fish it out of the SQS message and decode it:

queue = Queue('YOUR_QUEUE_NAME', sns=True)

When you use this option, the sns_message_id is added to the notification data, which can be used to make sure you only process each message once.

create

When you pass create=True then, if your SQS queue name is not found, a queue with that name will be created.

bulk_queue

You can pass this option another Queue, which will be checked only when the primary "priority" queue is empty. For example:

In [1]:   from sqs_queue import Queue

In [2]:   bulk = Queue(
   ...:       queue_name='bulk',
   ...:       create=True,
   ...:       poll_wait=2
   ...:   )

In [3]:   primary = Queue(
   ...:       queue_name='primary',
   ...:       bulk_queue=bulk,
   ...:       drain=True,
   ...:       create=True,
   ...:       poll_wait=2
   ...:   )

In [5]:   primary.publish('{"type": "priority", "id": 1}')
   ...:   bulk.publish('{"type": "bulk", "id": 1}')
   ...:   bulk.publish('{"type": "bulk", "id": 2}')

In [6]:   for msg in primary:
   ...:       print(msg)

{'type': 'priority', 'id': 1}
{'type': 'bulk', 'id': 1}
{'type': 'bulk', 'id': 2}

bulk_queue_check_pct

When using bulk_queue, the bulk queue is normally only checked when the primary queue is empty. With bulk_queue_check_pct, you can also randomly check the bulk queue after a percentage of non-empty primary queue polls:

primary = Queue(
    queue_name='primary',
    bulk_queue=bulk,
    bulk_queue_check_pct=25
)

This will check the bulk queue after approximately 25% of primary queue polls that returned messages, helping prevent bulk messages from being starved when the primary queue is continuously busy.

Release files for sqs-queue 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqs-queue 1.0.1
File Size Uploaded
sqs_queue-1.0.1.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqs-queue 1.0.1
File Interpreter ABI Platform
sqs_queue-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / sqs_queue-1.0.1.tar.gz

Download URL sqs_queue-1.0.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c138cd979cff7b987a036b263055335ccea71317fabcb9374ea0e3ce180ff0f1
BLAKE2b-256 checksum
How to use checksums
93be13d1a2277a3f7a2789c05d6ee9c18599c21486befe2e714e5dbf321335c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release files / sqs_queue-1.0.1-py3-none-any.whl

Download URL sqs_queue-1.0.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d10f9a853714b620294036bf42b42e46f0db2ddbbe1f69ae4dba71b5acfcadd
BLAKE2b-256 checksum
How to use checksums
e41b3265cbffc934399370eae473ab6095e02b784428a0ee7df7a93a3a663f24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

1 release file

0.6.4

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page