taskiq-aio-pika

0.6.0 · active · verified Thu Apr 16

taskiq-aio-pika is a broker implementation for the Taskiq asynchronous task queue, enabling it to use RabbitMQ as a message broker. It leverages aio-pika for asynchronous AMQP communication, providing robust and scalable message passing. The current version is 0.6.0 and it typically follows the release cadence of the main Taskiq library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `taskiq-aio-pika` by defining a `TaskiqSettings` class with an `AioPikaBroker` instance pointing to your RabbitMQ server. A simple task `process_data` is then defined using the broker's decorator. To run the worker, execute `taskiq worker your_module_name:settings_instance` in your terminal.

import os
from taskiq import TaskiqSettings
from taskiq_aio_pika import AioPikaBroker

# Configure RabbitMQ connection using environment variable or a default
RABBITMQ_URL = os.environ.get(
    "RABBITMQ_URL",
    "amqp://guest:guest@localhost:5672/"
)

class AppSettings(TaskiqSettings):
    """Taskiq settings with AioPikaBroker for RabbitMQ."""
    broker: AioPikaBroker = AioPikaBroker(
        url=RABBITMQ_URL,
    )

# Instantiate the settings
settings = AppSettings()

# Define a task using the broker's decorator
@settings.broker.task
async def process_data(data: str) -> str:
    """A simple task to process some data."""
    print(f"Processing: {data}")
    return f"Processed: {data.upper()}"

# To enqueue this task:
# import asyncio
# asyncio.run(process_data.kiq(data="hello world"))

# To run the worker (assuming this code is in `app_settings.py`):
# taskiq worker app_settings:settings

view raw JSON →