py-amqp

repository·main·Indexed 43 days ago

https://github.com/celery/py-amqp

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

Tokens
1.3K
Snippets
4
Records
13
Agent score
6%

What's inside py-amqp

  1. Use amqp.transport for AMQP communication

    main
    The amqp.transport module 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.
  2. Enable and handle Publisher Confirms

    main
    Publisher confirms can be enabled using Channel.confirm_select(). To handle confirmations, append a callback to Channel.events['basic_ack']. The callback signature is (delivery_tag, multiple).
  3. Configure heartbeats

    main
    To maintain a connection, you must handle heartbeats. You can either call Connection.heartbeat_tick(rate=2) at regular intervals (where the rate is half the heartbeat value) or use Connection.send_heartbeat manually.
  4. Enable Cython speedups

    main

    py-amqp has experimental support for speedups implemented via Cython. To enable them, you must set the CELERY_ENABLE_SPEEDUPS environment variable to true during the installation process.

    Using pip (from source):

    CELERY_ENABLE_SPEEDUPS=true pip install --no-binary :all: amqp

    Using setup.py:

    CELERY_ENABLE_SPEEDUPS=true python setup.py install
  5. Publish messages to a queue

    main

    To publish a message using the default exchange, create an amqp.Connection, open a channel, and use ch.basic_publish with a routing_key and an amqp.Message object.

    import amqp
    
    with amqp.Connection('broker.example.com') as c:
        ch = c.channel()
        ch.basic_publish(amqp.Message('Hello World'), routing_key='test')
  6. Consume messages with acknowledgments

    main

    To consume messages with manual acknowledgments, define a callback function that receives the message. Inside the callback, call ch.basic_ack(message.delivery_tag) to acknowledge receipt. Use c.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()
  7. Consume messages without acknowledgments

    main

    To consume messages where the broker does not expect an acknowledgment (no_ack mode), pass no_ack=True to ch.basic_consume. In this mode, the callback does not need to call basic_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()
  8. Publish messages with publisher confirms and virtual hosts

    main

    You can enable publisher confirms by setting confirm_publish=True in the amqp.Connection constructor. This allows you to verify that the broker has received the message. You can also specify a virtual_host and a specific exchange during 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')
  9. Use SSLTransport for secure AMQP connections

    main
    To establish an AMQP connection over SSL/TLS, use the SSLTransport class. 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).