{"id":1621,"library":"pika","title":"Pika Python AMQP Client","description":"Pika is a Python AMQP client library that provides both synchronous (BlockingConnection) and asynchronous (e.g., AsyncioConnection) interfaces for interacting with AMQP 0-9-1 brokers like RabbitMQ. The library is actively maintained, with regular patch and minor releases (currently at 1.3.2, with 1.4.0 in beta), addressing bug fixes, performance improvements, and Python compatibility.","status":"active","version":"1.3.2","language":"en","source_language":"en","source_url":"https://github.com/pika/pika","tags":["AMQP","RabbitMQ","messaging","async","blocking","producer","consumer"],"install":[{"cmd":"pip install pika","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The BlockingConnection class is directly available under the pika namespace since version 1.0.0.","wrong":"from pika.blocking_connection import BlockingConnection","symbol":"BlockingConnection","correct":"from pika import BlockingConnection"},{"symbol":"ConnectionParameters","correct":"from pika import ConnectionParameters"},{"note":"PlainCredentials is available directly in the pika namespace since 1.0.0, and ConnectionParameters no longer accepts the credentials argument directly.","wrong":"from pika import credentials.PlainCredentials","symbol":"PlainCredentials","correct":"from pika import PlainCredentials"},{"note":"Use the appropriate adapter for your asynchronous framework (e.g., asyncio, tornado).","symbol":"AsyncioConnection","correct":"from pika.adapters.asyncio_connection import AsyncioConnection"}],"quickstart":{"code":"import pika\nimport os\n\ndef callback(ch, method, properties, body):\n    print(f\" [x] Received {body.decode()}\")\n\n# Get RabbitMQ host from environment or default to localhost\nrabbitmq_host = os.environ.get('RABBITMQ_HOST', 'localhost')\n\nconnection = None\nchannel = None\ntry:\n    # Establish connection\n    connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitmq_host))\n    channel = connection.channel()\n\n    # Declare a queue\n    channel.queue_declare(queue='hello')\n\n    # Publish a message\n    channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')\n    print(\" [x] Sent 'Hello World!'\")\n\n    # Start consuming (example consumer setup)\n    # channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)\n    # print(' [*] Waiting for messages. To exit press CTRL+C')\n    # channel.start_consuming()\n\nfinally:\n    # Ensure channel and connection are closed\n    if channel and channel.is_open:\n        channel.close()\n    if connection and connection.is_open:\n        connection.close()\n","lang":"python","description":"This quickstart demonstrates a simple Pika producer using BlockingConnection to connect to a RabbitMQ instance (defaulting to localhost) and publish a message to a 'hello' queue. It also shows the basic setup for a consumer callback (commented out) and proper connection/channel closure."},"warnings":[{"fix":"For `ConnectionParameters`, create a `pika.credentials.PlainCredentials` object and pass it via `pika.ConnectionParameters(credentials=...)`. Update `on_message_callback` to accept `(ch, method, properties, body)`.","message":"Pika 1.0.0 introduced significant breaking changes, especially around connection parameters and callback signatures. `ConnectionParameters` no longer accepts `credentials` directly; `pika.PlainCredentials` (or similar) must be used. Also, the `on_message_callback` signature changed.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Ensure you understand Pika's different I/O adapters. Use `BlockingConnection` for synchronous applications or scripts, and an appropriate `pika.adapters` class for asynchronous applications integrated with an event loop (e.g., `asyncio`, `Tornado`). Do not call blocking methods on a channel managed by an async adapter outside of its event loop.","message":"Mixing blocking (BlockingConnection) and non-blocking (AsyncioConnection, TornadoConnection) I/O models in the same application without proper isolation can lead to deadlocks, unexpected behavior, or resource exhaustion. Choose one I/O model and stick to it, or use separate processes for different models.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always ensure that `channel.close()` and `connection.close()` are called, typically in a `finally` block, after your AMQP operations are complete or when an exception occurs. For asynchronous adapters, the `stop()` method often handles this gracefully.","message":"Failure to explicitly close channels and connections can lead to resource leaks (e.g., open file descriptors), hung processes, and issues with the RabbitMQ broker. Pika 1.0.0 and later require explicit closure for `BlockingConnection`.","severity":"gotcha","affected_versions":"All versions, especially >=1.0.0"},{"fix":"Implement robust error handling and reconnection logic in your application. For `BlockingConnection`, this typically involves a loop that attempts to re-establish the connection and channel after a delay. For asynchronous adapters, specific reconnection examples are often provided in the Pika documentation.","message":"Pika does not automatically handle connection or channel recovery by default. Network outages, broker restarts, or protocol errors will cause `pika.exceptions.ConnectionClosed` or `pika.exceptions.ChannelClosed`.","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"}