async-sqs-consumer


Nameasync-sqs-consumer JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/drestrepom/async_sqs_consumer
SummaryNone
upload_time2024-11-29 22:05:35
maintainerNone
docs_urlNone
authorDiego Restrepo
requires_python<4.0,>=3.9
licenseNone
keywords aws aws sqs sqs consumer async worker
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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`

```python
# 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

```python
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

```python
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

```json
{
    "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"],
}
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/drestrepom/async_sqs_consumer",
    "name": "async-sqs-consumer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "AWS, AWS, SQS, sqs, consumer, async, worker",
    "author": "Diego Restrepo",
    "author_email": "drestrepo@fluidattacks.com",
    "download_url": "https://files.pythonhosted.org/packages/c0/b7/cbf46ac6d078c3bd0fee21ed268c57057a2c416216b0b864f9007cefcd51/async_sqs_consumer-1.0.2.tar.gz",
    "platform": null,
    "description": "# Async SQS consumer\n\nPython asynchronous (**async** / **await**) worker for consuming messages\nfrom AWS SQS.\n\nThis is a hobby project, if you find the project interesting\nany contribution is welcome.\n\n## Usage\n\nYou must create an instance of the worker with the url of the queue.\n\nAws credentials are taken from environment variables, you must set the\nfollowing environment variables. Or you can provide a Context object with the\naws credentials `async_sqs_consumer.types.Context`\n\n- `AWS_ACCESS_KEY_ID`\n- `AWS_SECRET_ACCESS_KEY`\n\nExample:\n\nYou can get the queue url with the follow aws cli command\n`aws sqs get-queue-url --queue-name xxxxxx`\n\n```python\n# test_worker.py\n\nfrom async_sqs_consumer.worker import (\n    Worker,\n)\n\nworker = Worker(\n    queue_url=\"https://sqs.us-east-1.amazonaws.com/xxxxxxx/queue_name\"\n)\n\n\n@worker.task(\"report\")\nasync def report(text: str) -> None:\n    print(text)\n\nif __name__: \"__main__\":\n    worker.start()\n```\n\nNow you can initialize the worker `python test_worker.py`\n\nNow you need to post a message for the worker to process\n\n```python\nimport json\nimport boto3\nimport uuid\n\nclient = boto3.client(\"sqs\")\n\nclient.send_message(\n    QueueUrl=\"https://sqs.us-east-1.amazonaws.com/xxxxxxx/queue_name\",\n    MessageBody=json.dumps(\n        {\n            \"task\": \"report\",\n            \"id\": uuid.uuid4().hex,\n            \"args\": [\"hello world\"],\n        }\n    ),\n)\n```\n\nOr you can use aioboto3\n\n```python\nimport asyncio\nimport json\nimport aioboto3\nimport uuid\n\n\nasync def main() -> None:\n    session = aioboto3.Session()\n    async with session.client(\"sqs\") as client:\n        await client.send_message(\n            QueueUrl=\"https://sqs.us-east-1.amazonaws.com/xxxxxxx/queue_name\",\n            MessageBody=json.dumps(\n                {\n                    \"task\": \"report\",\n                    \"id\": uuid.uuid4().hex,\n                    \"args\": [\"hello world\"],\n                }\n            ),\n        )\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n```\n\nTo publish the messages they must have the following structure\n\n```json\n{\n    \"type\": \"object\",\n    \"properties\": {\n        \"task\": {\"type\": \"string\"},\n        \"id\": {\"type\": \"string\"},\n        \"args\": {\"type\": \"array\"},\n        \"kwargs\": {\"type\": \"object\"},\n        \"retries\": {\"type\": \"number\"},\n        \"eta\": {\"type\": \"string\"},\n        \"expires\": {\"type\": \"string\"},\n    },\n    \"required\": [\"task\", \"id\"],\n}\n```\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "1.0.2",
    "project_urls": {
        "Homepage": "https://github.com/drestrepom/async_sqs_consumer",
        "Repository": "https://github.com/drestrepom/async_sqs_consumer"
    },
    "split_keywords": [
        "aws",
        " aws",
        " sqs",
        " sqs",
        " consumer",
        " async",
        " worker"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "39eead6c5084bfa445a97c6b1beb82c1a3156318dcdced1e2b916860926e698e",
                "md5": "5caa773e18414b74781721a0d7a228b0",
                "sha256": "096180ad7adcc3ba7117986b3670527607a649dc00b151185a95dffd2418bc07"
            },
            "downloads": -1,
            "filename": "async_sqs_consumer-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5caa773e18414b74781721a0d7a228b0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 11192,
            "upload_time": "2024-11-29T22:05:27",
            "upload_time_iso_8601": "2024-11-29T22:05:27.955884Z",
            "url": "https://files.pythonhosted.org/packages/39/ee/ad6c5084bfa445a97c6b1beb82c1a3156318dcdced1e2b916860926e698e/async_sqs_consumer-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c0b7cbf46ac6d078c3bd0fee21ed268c57057a2c416216b0b864f9007cefcd51",
                "md5": "aab406fb5fa211453af61905fb73e90f",
                "sha256": "481da0a9317be7943849d3c8236e5ad42a699820297a93a65cc837a2f9b8e32e"
            },
            "downloads": -1,
            "filename": "async_sqs_consumer-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "aab406fb5fa211453af61905fb73e90f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 9216,
            "upload_time": "2024-11-29T22:05:35",
            "upload_time_iso_8601": "2024-11-29T22:05:35.499041Z",
            "url": "https://files.pythonhosted.org/packages/c0/b7/cbf46ac6d078c3bd0fee21ed268c57057a2c416216b0b864f9007cefcd51/async_sqs_consumer-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-29 22:05:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "drestrepom",
    "github_project": "async_sqs_consumer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "async-sqs-consumer"
}
        
Elapsed time: 0.63840s