py-amqp
repository·main·Indexed 43 days ago
https://github.com/celery/py-amqpA pure Python AMQP 0.9.1 client library maintained by the Celery project. It serves as a lightweight alternative to librabbitmq and is used extensively by the kombu library. It provides core transport layers via TCPTransport and SSLTransport, and supports features such as publisher confirms, manual message acknowledgments, and optional Cython speedups.
What's inside py-amqp
- py-amqp is a Python client library for the AMQP 0.9.1 protocol. It is used to interact with AMQP brokers. It is distributed via PyPI and supports multiple Python implementations.
Use amqp.transport for AMQP communication
mainTheamqp.transportmodule provides the core transport layer for the Python AMQP 0.9.1 client library. It defines the mechanisms for moving data over different network protocols. Users can utilize specific transport implementations depending on their security and network requirements, such as standard TCP or encrypted SSL/TLS connections.Enable and handle Publisher Confirms
mainPublisher confirms can be enabled usingChannel.confirm_select(). To handle confirmations, append a callback toChannel.events['basic_ack']. The callback signature is(delivery_tag, multiple).Handle Consumer Cancel Notifications
mainBy default, a consumer cancellation results in aChannelError. However, you can prevent this by passing anon_cancelcallback to thebasic_consumemethod.Configure heartbeats
mainTo maintain a connection, you must handle heartbeats. You can either callConnection.heartbeat_tick(rate=2)at regular intervals (where the rate is half the heartbeat value) or useConnection.send_heartbeatmanually.Enable Cython speedups
mainpy-amqp has experimental support for speedups implemented via Cython. To enable them, you must set the
CELERY_ENABLE_SPEEDUPSenvironment variable totrueduring the installation process.Using pip (from source):
CELERY_ENABLE_SPEEDUPS=true pip install --no-binary :all: amqpUsing setup.py:
CELERY_ENABLE_SPEEDUPS=true python setup.py installPublish messages to a queue
mainTo publish a message using the default exchange, create an
amqp.Connection, open a channel, and usech.basic_publishwith arouting_keyand anamqp.Messageobject.import amqp with amqp.Connection('broker.example.com') as c: ch = c.channel() ch.basic_publish(amqp.Message('Hello World'), routing_key='test')Consume messages with acknowledgments
mainTo consume messages with manual acknowledgments, define a callback function that receives the
message. Inside the callback, callch.basic_ack(message.delivery_tag)to acknowledge receipt. Usec.drain_events()in a loop to process incoming events.import amqp with amqp.Connection('broker.example.com') as c: ch = c.channel() def on_message(message): print('Received message (delivery tag: {}): {}'.format(message.delivery_tag, message.body)) ch.basic_ack(message.delivery_tag) ch.basic_consume(queue='test', callback=on_message) while True: c.drain_events()Consume messages without acknowledgments
mainTo consume messages where the broker does not expect an acknowledgment (no_ack mode), pass
no_ack=Truetoch.basic_consume. In this mode, the callback does not need to callbasic_ack.import amqp with amqp.Connection('broker.example.com') as c: ch = c.channel() def on_message(message): print('Received message (delivery tag: {}): {}'.format(message.delivery_tag, message.body)) ch.basic_consume(queue='test', callback=on_message, no_ack=True) while True: c.drain_events()Publish messages with publisher confirms and virtual hosts
mainYou can enable publisher confirms by setting
confirm_publish=Truein theamqp.Connectionconstructor. This allows you to verify that the broker has received the message. You can also specify avirtual_hostand a specificexchangeduring connection.import amqp with amqp.Connection( 'broker.example.com', exchange='test_exchange', confirm_publish=True, virtual_host='test_vhost' ) as c: ch = c.channel() ch.basic_publish(amqp.Message('Hello World'), routing_key='test')Use SSLTransport for secure AMQP connections
mainTo establish an AMQP connection over SSL/TLS, use theSSLTransportclass. This class handles the wrapping of sockets with an SSL context to ensure encrypted communication. It includes internal methods for managing the SSL context (_wrap_context) and handling Server Name Indication (_wrap_socket_sni).Use TCPTransport for standard AMQP connections
mainFor standard, unencrypted AMQP communication over a network, use theTCPTransportclass. This is the base implementation for transport over raw TCP sockets.