# Quick Start
## Background
[Dramatiq](https://dramatiq.io/) is a background task-processing library for Python with a focus on simplicity, reliability and performance.
This package, [async-dramatiq](https://pypi.org/project/async-dramatiq/), extends Dramatiq to provide the following:
1. Support for Asyncio ( [issue #238](https://github.com/Bogdanp/dramatiq/issues/238) )
2. Message scheduling support ( [scheduling cookbook](https://dramatiq.io/cookbook.html#scheduling-messages) )
## Setup
To provide async support for your actors all you need to do is add the `AsyncMiddleware` to your broker.
#### RabbitMQ Broker
```
import dramatiq
from dramatiq.brokers.rabbitmq import RabbitmqBroker
rabbitmq_broker = RabbitmqBroker(host="rabbitmq")
rabbitmq_broker.add_middleware(AsyncMiddleware()) # <--- Here
dramatiq.set_broker(rabbitmq_broker)
```
#### Redis Broker
```
import dramatiq
from dramatiq.brokers.redis import RedisBroker
redis_broker = RedisBroker(host="redis")
redis_broker.add_middleware(AsyncMiddleware()) # <--- Here
dramatiq.set_broker(redis_broker)
```
## Running
#### The Scheduler
We leverage [apscheduler](https://pypi.org/project/APScheduler/) as our scheduling system. Check out [run_scheduler.py](examples/worker_heartbeat/run_scheduler.py) for an example of running this scheduler.
#### Dramatiq Worker
For more details check out the official guide to [dramatiq](https://dramatiq.io/guide.html#workers) or [docker-compose.yaml](examples/worker_heartbeat/docker-compose.yaml) for a specific example.
## Example
Play around with [worker-heartbeat-example](examples/worker_heartbeat/README.md). A functioning and featured example implementation.
----
# Async Middleware
`AsyncMiddleware` will start a `AsyncWorker` which will be used to run the event loop. This event loop will be shared across the Worker threads.
### Startup and Shutdown Events
To startup and shutdown any resources the `AsyncMiddleware` provides two hooks:
1. Before the event loop is started
2. After the event loop is stopped
To allow for standing up or tearing down of shared async resources
#### Example
```
from async_dramatiq.middleware import AsyncMiddleware
async def startup() -> None:
"""This function should contain your resource initialization code."""
pass
async def shutdown() -> None:
"""This function should contain your resource teardown code."""
pass
class MyAsyncMiddleware(AsyncMiddleware):
def before_async_worker_thread_startup(
self, _: RabbitmqBroker, thread: AsyncWorker, **kwargs: dict[str, Any]
) -> None:
thread.event_loop.run_until_complete(startup())
def after_async_worker_thread_shutdown(
self, _: RabbitmqBroker, thread: AsyncWorker, **kwargs: dict[str, Any]
) -> None:
thread.event_loop.run_until_complete(shutdown())
thread.event_loop.close()
```
# Async Actor
The async actor, `async_actor`, acts as a thin wrapper around the Dramatiq actor providing a variety of new functionality.
## Interval Jobs
Run a job at some interval
```
@async_actor(interval=timedelta(seconds=5))
def run_every_5_seconds() -> None:
pass
```
## Cron Jobs
Run a job on a crontab ( See https://crontab.guru/. )
```
@async_actor(interval="0 0 * * *")
def run_at_midnight() -> None:
pass
```
Raw data
{
"_id": null,
"home_page": "https://github.com/motherofcoconuts/async-dramatiq",
"name": "async-dramatiq",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10.1,<4.0.0",
"maintainer_email": "",
"keywords": "",
"author": "Ryan Houlihan",
"author_email": "ryan@rhoulihan.com",
"download_url": "https://files.pythonhosted.org/packages/c3/c3/99860188e308663b20da2ebf4e69f9dadc7c6d5710bce054d870458dce8c/async_dramatiq-0.1.8.tar.gz",
"platform": null,
"description": "# Quick Start\n## Background\n[Dramatiq](https://dramatiq.io/) is a background task-processing library for Python with a focus on simplicity, reliability and performance.\n\nThis package, [async-dramatiq](https://pypi.org/project/async-dramatiq/), extends Dramatiq to provide the following:\n 1. Support for Asyncio ( [issue #238](https://github.com/Bogdanp/dramatiq/issues/238) )\n 2. Message scheduling support ( [scheduling cookbook](https://dramatiq.io/cookbook.html#scheduling-messages) )\n\n## Setup\nTo provide async support for your actors all you need to do is add the `AsyncMiddleware` to your broker.\n#### RabbitMQ Broker\n\n```\nimport dramatiq\nfrom dramatiq.brokers.rabbitmq import RabbitmqBroker\n\nrabbitmq_broker = RabbitmqBroker(host=\"rabbitmq\")\nrabbitmq_broker.add_middleware(AsyncMiddleware()) # <--- Here\ndramatiq.set_broker(rabbitmq_broker)\n```\n\n#### Redis Broker\n\n```\nimport dramatiq\nfrom dramatiq.brokers.redis import RedisBroker\n\nredis_broker = RedisBroker(host=\"redis\")\nredis_broker.add_middleware(AsyncMiddleware()) # <--- Here\ndramatiq.set_broker(redis_broker)\n```\n\n## Running\n#### The Scheduler\nWe leverage [apscheduler](https://pypi.org/project/APScheduler/) as our scheduling system. Check out [run_scheduler.py](examples/worker_heartbeat/run_scheduler.py) for an example of running this scheduler.\n#### Dramatiq Worker\nFor more details check out the official guide to [dramatiq](https://dramatiq.io/guide.html#workers) or [docker-compose.yaml](examples/worker_heartbeat/docker-compose.yaml) for a specific example.\n\n\n## Example\nPlay around with [worker-heartbeat-example](examples/worker_heartbeat/README.md). A functioning and featured example implementation.\n\n----\n# Async Middleware\n`AsyncMiddleware` will start a `AsyncWorker` which will be used to run the event loop. This event loop will be shared across the Worker threads. \n### Startup and Shutdown Events\nTo startup and shutdown any resources the `AsyncMiddleware` provides two hooks:\n1. Before the event loop is started\n2. After the event loop is stopped\nTo allow for standing up or tearing down of shared async resources\n#### Example\n```\nfrom async_dramatiq.middleware import AsyncMiddleware\n\nasync def startup() -> None:\n \"\"\"This function should contain your resource initialization code.\"\"\"\n pass\n\nasync def shutdown() -> None:\n \"\"\"This function should contain your resource teardown code.\"\"\"\n pass\n\n\nclass MyAsyncMiddleware(AsyncMiddleware):\n def before_async_worker_thread_startup(\n self, _: RabbitmqBroker, thread: AsyncWorker, **kwargs: dict[str, Any]\n ) -> None:\n thread.event_loop.run_until_complete(startup())\n\n def after_async_worker_thread_shutdown(\n self, _: RabbitmqBroker, thread: AsyncWorker, **kwargs: dict[str, Any]\n ) -> None:\n thread.event_loop.run_until_complete(shutdown())\n thread.event_loop.close()\n```\n\n# Async Actor\nThe async actor, `async_actor`, acts as a thin wrapper around the Dramatiq actor providing a variety of new functionality.\n\n## Interval Jobs\nRun a job at some interval\n```\n@async_actor(interval=timedelta(seconds=5))\ndef run_every_5_seconds() -> None:\n pass\n```\n\n## Cron Jobs\nRun a job on a crontab ( See https://crontab.guru/. )\n```\n@async_actor(interval=\"0 0 * * *\")\ndef run_at_midnight() -> None:\n pass\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Dramatiq with Asyncio support and some other goodies",
"version": "0.1.8",
"project_urls": {
"Bug Tracker": "https://github.com/motherofcoconuts/async-dramatiq/issues",
"Homepage": "https://github.com/motherofcoconuts/async-dramatiq"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5021eb416537db4ce38af133b2e264f6fcd6133d816cf8d324f9b0271c576a65",
"md5": "407cab696715380ee17dc0f7de07ace0",
"sha256": "7306b40a31fb182970c0509d2c097fcb132ddb3d6a77860408e07f135cb35fc1"
},
"downloads": -1,
"filename": "async_dramatiq-0.1.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "407cab696715380ee17dc0f7de07ace0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10.1,<4.0.0",
"size": 10121,
"upload_time": "2023-05-27T09:01:18",
"upload_time_iso_8601": "2023-05-27T09:01:18.180290Z",
"url": "https://files.pythonhosted.org/packages/50/21/eb416537db4ce38af133b2e264f6fcd6133d816cf8d324f9b0271c576a65/async_dramatiq-0.1.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c3c399860188e308663b20da2ebf4e69f9dadc7c6d5710bce054d870458dce8c",
"md5": "3311861755b3337589c3faa90c4a8465",
"sha256": "0e26ef0ee6c2697b85997007aab1ebf705e547b9b3edf1034a6bcf06477fab1a"
},
"downloads": -1,
"filename": "async_dramatiq-0.1.8.tar.gz",
"has_sig": false,
"md5_digest": "3311861755b3337589c3faa90c4a8465",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10.1,<4.0.0",
"size": 10763,
"upload_time": "2023-05-27T09:01:19",
"upload_time_iso_8601": "2023-05-27T09:01:19.911635Z",
"url": "https://files.pythonhosted.org/packages/c3/c3/99860188e308663b20da2ebf4e69f9dadc7c6d5710bce054d870458dce8c/async_dramatiq-0.1.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-27 09:01:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "motherofcoconuts",
"github_project": "async-dramatiq",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "async-dramatiq"
}