Skip to main content

Async SQS consumer

Python asynchronous (async / await) worker for consuming messages from AWS SQS.

This is a hobby project, if you find the project interesting any contribution is welcome.

Usage

You must create an instance of the worker with the url of the queue.

Aws credentials are taken from environment variables, you must set the following environment variables. Or you can provide a Context object with the aws credentials async_sqs_consumer.types.Context

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

Example:

You can get the queue url with the follow aws cli command aws sqs get-queue-url --queue-name xxxxxx

# test_worker.py

from async_sqs_consumer.worker import (
    Worker,
)

worker = Worker(
    queue_url="https://sqs.us-east-1.amazonaws.com/xxxxxxx/queue_name"
)


@worker.task("report")
async def report(text: str) -> None:
    print(text)

if __name__: "__main__":
    worker.start()

Now you can initialize the worker python test_worker.py

Now you need to post a message for the worker to process

import json
import boto3
import uuid

client = boto3.client("sqs")

client.send_message(
    QueueUrl="https://sqs.us-east-1.amazonaws.com/xxxxxxx/queue_name",
    MessageBody=json.dumps(
        {
            "task": "report",
            "id": uuid.uuid4().hex,
            "args": ["hello world"],
        }
    ),
)

Or you can use aioboto3

import asyncio
import json
import aioboto3
import uuid


async def main() -> None:
    session = aioboto3.Session()
    async with session.client("sqs") as client:
        await client.send_message(
            QueueUrl="https://sqs.us-east-1.amazonaws.com/xxxxxxx/queue_name",
            MessageBody=json.dumps(
                {
                    "task": "report",
                    "id": uuid.uuid4().hex,
                    "args": ["hello world"],
                }
            ),
        )


if __name__ == "__main__":
    asyncio.run(main())

To publish the messages they must have the following structure

{
    "type": "object",
    "properties": {
        "task": {"type": "string"},
        "id": {"type": "string"},
        "args": {"type": "array"},
        "kwargs": {"type": "object"},
        "retries": {"type": "number"},
        "eta": {"type": "string"},
        "expires": {"type": "string"},
    },
    "required": ["task", "id"],
}

Metadata

Release files for async_sqs_consumer 1.1.0

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

Source distribution (sdist)

Source distribution for async_sqs_consumer 1.1.0
File Size Uploaded Provenance Actions
async_sqs_consumer-1.1.0.tar.gz 8.6 kB Without provenance Details for async_sqs_consumer-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for async_sqs_consumer 1.1.0
File Interpreter ABI Platform Actions
async_sqs_consumer-1.1.0-py3-none-any.whl Python 3 none any Details for async_sqs_consumer-1.1.0-py3-none-any.whl

Total release size: 20.1 kB

Release files / async_sqs_consumer-1.1.0.tar.gz

Download URL async_sqs_consumer-1.1.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
eef32dede34524081f20d0b844c3834b4666ca67efafc423d6403bb13268a3c3
BLAKE2b-256 checksum
How to use checksums
2e39dc07470127ff481c90672b7ff541b5524ffd52add5553996e9718e00248e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / async_sqs_consumer-1.1.0-py3-none-any.whl

Download URL async_sqs_consumer-1.1.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24751c7eb48271e77aecd539df684b1229ea681238dd984680258527579f8626
BLAKE2b-256 checksum
How to use checksums
eda3a991605ed11b0759ba2b870338040a8eefbdecb960404a8f96a4a834de0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for async_sqs_consumer 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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