{"id":1897,"library":"aiormq","title":"aiormq: Asynchronous AMQP Client","description":"aiormq is a pure Python asynchronous client library for the AMQP 0.9.1 protocol, commonly used for interacting with RabbitMQ. It provides a non-blocking interface leveraging `asyncio`. The current version is 6.9.4, and it maintains a steady release cadence with frequent patch and minor updates addressing bug fixes and performance improvements.","status":"active","version":"6.9.4","language":"en","source_language":"en","source_url":"https://github.com/mosquito/aiormq","tags":["AMQP","RabbitMQ","asyncio","message queue","messaging"],"install":[{"cmd":"pip install aiormq","lang":"bash","label":"Install aiormq"}],"dependencies":[{"reason":"Core dependency for AMQP 0.9.1 protocol framing.","package":"pamqp","optional":false}],"imports":[{"symbol":"connect","correct":"from aiormq import connect"},{"note":"While Channel is part of the internal structure, for type hinting, import from `aiormq.abc`.","symbol":"Channel","correct":"from aiormq.abc import Channel"},{"note":"Required for type hinting message callbacks during consumption.","symbol":"IncomingMessage","correct":"from aiormq.abc import IncomingMessage"}],"quickstart":{"code":"import asyncio\nimport os\nfrom aiormq import connect\nfrom aiormq.abc import IncomingMessage\n\n# Get AMQP URL from environment variable, default to local RabbitMQ\nAMQP_URL = os.environ.get('AMQP_URL', 'amqp://guest:guest@localhost/')\nQUEUE_NAME = 'aiormq_test_queue'\n\nasync def on_message(message: IncomingMessage):\n    \"\"\"Callback for consuming messages.\"\"\"\n    print(f\"[x] Received: {message.body.decode()}\")\n    await message.ack() # Acknowledge the message\n\nasync def main():\n    connection = None\n    try:\n        # Establish connection\n        print(f\"[*] Connecting to {AMQP_URL}...\")\n        connection = await connect(AMQP_URL)\n        print(\"[*] Connection established.\")\n\n        # Create a channel\n        channel = await connection.channel()\n        print(\"[*] Channel created.\")\n\n        # Declare a queue (idempotent operation)\n        await channel.queue_declare(QUEUE_NAME)\n        print(f\"[*] Queue '{QUEUE_NAME}' declared.\")\n\n        # Publish a message\n        message_body = b\"Hello, aiormq world!\"\n        await channel.basic_publish(\n            exchange='',\n            routing_key=QUEUE_NAME,\n            body=message_body\n        )\n        print(f\"[x] Published message: '{message_body.decode()}'\")\n\n        # Start consuming messages\n        consumer_tag = await channel.basic_consume(QUEUE_NAME, on_message)\n        print(f\"[*] Consuming from '{QUEUE_NAME}'. Consumer tag: {consumer_tag}\")\n\n        # Keep the consumer running for a short period (e.g., 5 seconds)\n        print(\"[*] Waiting for messages... Press Ctrl+C to exit\")\n        await asyncio.sleep(5)\n\n        # Stop consuming\n        await channel.basic_cancel(consumer_tag)\n        print(f\"[*] Consumer '{consumer_tag}' cancelled.\")\n\n    except Exception as e:\n        print(f\"[!] An error occurred: {e}\")\n    finally:\n        if connection:\n            print(\"[*] Closing connection...\")\n            await connection.close()\n            print(\"[*] Connection closed.\")\n\nif __name__ == '__main__':\n    asyncio.run(main())\n","lang":"python","description":"This quickstart demonstrates how to connect to an AMQP broker (like RabbitMQ), declare a queue, publish a message, and consume messages asynchronously. It uses `os.environ.get` for the AMQP URL, making it easy to configure without hardcoding credentials. Remember to have a RabbitMQ instance running at `localhost:5672` or specify a different `AMQP_URL`."},"warnings":[{"fix":"Upgrade to Python 3.8+ or pin `aiormq` to a version less than 6.8.0 in your `requirements.txt`.","message":"Python 3.7 support was dropped in aiormq version 6.8.0. If you are using Python 3.7, you must upgrade your Python version or pin `aiormq<6.8.0`.","severity":"breaking","affected_versions":">=6.8.0"},{"fix":"Always wrap your AMQP operations in `try...finally` blocks to ensure `await connection.close()` and `await channel.close()` are called, even if exceptions occur. For channels, ensure they are closed before the connection.","message":"Properly closing connections and channels is crucial in asynchronous applications to prevent resource leaks and ensure graceful shutdown. Failing to `await connection.close()` and `await channel.close()` can lead to hung connections or unreleased resources.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure your AMQP connection URL is properly URL-encoded, especially for parts like VHosts or usernames/passwords that might contain special characters. Test complex URLs thoroughly.","message":"While `aiormq` aims to handle AMQP URLs robustly, be mindful of special characters, complex VHosts, or non-standard query parameters in your connection string. Historically, there have been minor fixes related to URL parsing (e.g., slash unquoting in 6.8.1).","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}