Kombu

5.6.2 · active · verified Sat Mar 28

Kombu is an asynchronous messaging library for Python, providing a high-level interface to the AMQP protocol and supporting various message brokers. It abstracts away the complexities of message passing, allowing developers to focus on application logic. Currently at version 5.6.2, Kombu maintains an active development and release cadence, often aligning with updates in its parent project, Celery.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the simple interface of Kombu to send and receive a message using a Redis broker. It creates a `SimpleQueue` for publishing and consuming, showcasing how to put a message and then retrieve it, acknowledging its receipt.

import datetime
from kombu import Connection

BROKER_URL = 'redis://localhost:6379/0' # Use os.environ.get('BROKER_URL', '...') in production

# --- Publisher ---
with Connection(BROKER_URL) as conn:
    simple_queue = conn.SimpleQueue('simple_queue')
    message_payload = f'helloworld, sent at {datetime.datetime.now()}'
    simple_queue.put(message_payload)
    print(f'Sent: {message_payload}')
    simple_queue.close()

# --- Consumer ---
with Connection(BROKER_URL) as conn:
    simple_queue = conn.SimpleQueue('simple_queue')
    try:
        message = simple_queue.get(block=True, timeout=5)
        print(f'Received: {message.payload}')
        message.ack()
    except conn.Empty: # Or kombu.exceptions.TimeoutError
        print('No messages received within timeout.')
    finally:
        simple_queue.close()

view raw JSON →