{"id":1895,"library":"aio-pika","title":"aio-pika","description":"aio-pika is an asyncio-native Python library providing a high-level wrapper around `aiormq` for interacting with AMQP brokers like RabbitMQ. It simplifies common messaging patterns, offering both basic and robust (auto-reconnecting) connections. The library is actively maintained with frequent patch and minor releases, ensuring compatibility and bug fixes.","status":"active","version":"9.6.2","language":"en","source_language":"en","source_url":"https://github.com/mosquito/aio-pika","tags":["async","amqp","rabbitmq","message-queue","asyncio"],"install":[{"cmd":"pip install aio-pika","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"aio-pika is a high-level wrapper around the lower-level `aiormq` library, which handles the AMQP protocol communication.","package":"aiormq","optional":false}],"imports":[{"symbol":"connect","correct":"from aio_pika import connect"},{"note":"Use `connect_robust` for connections that automatically reconnect upon network issues, highly recommended for production.","symbol":"connect_robust","correct":"from aio_pika import connect_robust"},{"symbol":"Message","correct":"from aio_pika import Message"},{"symbol":"IncomingMessage","correct":"from aio_pika import IncomingMessage"}],"quickstart":{"code":"import asyncio\nfrom aio_pika import connect_robust, Message, IncomingMessage\n\nasync def main():\n    # Connect to RabbitMQ (replace with your actual connection string)\n    connection_string = \"amqp://guest:guest@localhost/\"\n    connection = await connect_robust(connection_string)\n\n    async with connection:\n        channel = await connection.channel() # type: ignore\n\n        # Declare a queue\n        queue_name = \"my_queue\"\n        queue = await channel.declare_queue(queue_name, auto_delete=True)\n\n        # Consumer function\n        async def on_message_received(message: IncomingMessage):\n            async with message.process():\n                print(f\" [x] Received {message.body.decode()}\")\n                # Acknowledge the message explicitly\n\n        # Start consuming messages\n        print(f\" [*] Waiting for messages on {queue_name}. To exit press CTRL+C\")\n        await queue.consume(on_message_received, no_ack=False)\n\n        # Publish a message\n        message_body = b\"Hello, aio-pika!\"\n        await channel.default_exchange.publish(\n            Message(message_body),\n            routing_key=queue_name\n        )\n        print(f\" [x] Sent '{message_body.decode()}'\")\n\n        # Keep the consumer running for a bit\n        await asyncio.sleep(5)\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n","lang":"python","description":"This quickstart demonstrates how to establish a robust connection, declare a queue, publish a message, and consume it using aio-pika. It highlights the use of `connect_robust` for production readiness and explicit message acknowledgment. Make sure a RabbitMQ instance is running on `localhost`."},"warnings":[{"fix":"Upgrade your Python environment to 3.8+ or pin `aio-pika<9.4.0` in your dependencies.","message":"aio-pika v9.4.0 dropped support for Python 3.7. Users running Python 3.7 must pin their `aio-pika` version to `<9.4.0`.","severity":"breaking","affected_versions":">=9.4.0"},{"fix":"Use `aio_pika.Channel.get_underlay_channel()` instead if you need to access the raw `aiormq` channel object. It's generally recommended to stick to `aio-pika`'s higher-level API.","message":"The `aio_pika.Channel.channel` property (which exposed the underlying `aiormq` channel) was deprecated in version 9.1.0.","severity":"deprecated","affected_versions":">=9.1.0"},{"fix":"`connect_robust()` automatically handles connection and channel re-establishment upon network disconnections, making your application more resilient to transient failures. `connect()` does not provide this robustness.","message":"For production applications, it is highly recommended to use `aio_pika.connect_robust()` instead of `aio_pika.connect()`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to `aio-pika` version 9.4.2 or higher to ensure proper message `nack` behavior during consumer cancellation.","message":"Prior to version 9.4.2, messages might not have been correctly `nack`ed (negatively acknowledged) upon cancellation of a consumer subscription, potentially leading to messages being lost or stuck.","severity":"gotcha","affected_versions":"<9.4.2"},{"fix":"Always use `async with message.process():` for automatic acknowledgment/rejection on task completion/failure, or call `message.ack()`/`message.nack()` manually to prevent messages from remaining in the queue indefinitely or being redelivered unnecessarily.","message":"Messages consumed from a queue using `consume()` or `get()` must be explicitly acknowledged (`message.ack()`) or rejected (`message.nack()`, `message.reject()`) unless `no_ack=True` is set during consumption.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}