Registry / communication / aio-pika

aio-pika

JSON →
library10.0.1pypypi✓ verified 24d ago

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.

pip install aio-pika
INSTALL
IMPORT
SIG · AIO-PIKA
A
aio-pika
communicationpythonv10.0.1
Install
2.9s avg
Import
427ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v9.6.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.450s · 21.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.9s · import 0.404s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

connect
from aio_pika import connect
connect_robust
from aio_pika import connect_robust
Use `connect_robust` for connections that automatically reconnect upon network issues, highly recommended for production.
Message
from aio_pika import Message
IncomingMessage
from aio_pika import IncomingMessage

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

import asyncio from aio_pika import connect_robust, Message, IncomingMessage async def main(): # Connect to RabbitMQ (replace with your actual connection string) connection_string = "amqp://guest:guest@localhost/" connection = await connect_robust(connection_string) async with connection: channel = await connection.channel() # type: ignore # Declare a queue queue_name = "my_queue" queue = await channel.declare_queue(queue_name, auto_delete=True) # Consumer function async def on_message_received(message: IncomingMessage): async with message.process(): print(f" [x] Received {message.body.decode()}") # Acknowledge the message explicitly # Start consuming messages print(f" [*] Waiting for messages on {queue_name}. To exit press CTRL+C") await queue.consume(on_message_received, no_ack=False) # Publish a message message_body = b"Hello, aio-pika!" await channel.default_exchange.publish( Message(message_body), routing_key=queue_name ) print(f" [x] Sent '{message_body.decode()}'") # Keep the consumer running for a bit await asyncio.sleep(5) if __name__ == "__main__": asyncio.run(main())
Debug
Known issues
breakingaio-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`.
fix
Upgrade your Python environment to 3.8+ or pin `aio-pika<9.4.0` in your dependencies.
affects: >=9.4.0
deprecatedThe `aio_pika.Channel.channel` property (which exposed the underlying `aiormq` channel) was deprecated in version 9.1.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.
affects: >=9.1.0
gotchaFor production applications, it is highly recommended to use `aio_pika.connect_robust()` instead of `aio_pika.connect()`.
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.
affects: All versions
gotchaPrior 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.
fix
Upgrade to `aio-pika` version 9.4.2 or higher to ensure proper message `nack` behavior during consumer cancellation.
affects: <9.4.2
gotchaMessages 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.
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'aio_pika'
The 'aio_pika' module is not installed in the Python environment.
fix
Install the module using pip: 'pip install aio-pika'.
AttributeError: module 'aio_pika' has no attribute 'connect_robust'
The 'connect_robust' function is not available in the installed version of 'aio_pika'.
fix
Ensure you have the latest version of 'aio-pika' installed: 'pip install --upgrade aio-pika'.
ConnectionError: [Errno 111] Connection refused
The application is unable to connect to the RabbitMQ server, possibly because the server is not running or is unreachable.
fix
Verify that the RabbitMQ server is running and accessible at the specified host and port.
RuntimeError: Event loop is closed
The asyncio event loop was closed before all asynchronous tasks completed.
fix
Ensure that the event loop remains open until all tasks are finished, and avoid closing it prematurely.
ValueError: Invalid URL scheme
The connection URL provided to 'aio_pika' uses an unsupported or incorrect scheme.
fix
Use a valid AMQP URL scheme, such as 'amqp://user:password@host:port/'.
Upgrade
Version history
10.0.1latest on PyPI · released Jul 9, 2026
Audit
Dependencies
aiormqrequiredaio-pika is a high-level wrapper around the lower-level `aiormq` library, which handles the AMQP protocol communication.
Agent activity
74 hits · last 30 days
node
62
OpenAI (training)
2
Resources
aio-pika — pip install aio-pika · libregistry