Examples

Hello World

import asyncio
import asynqp


@asyncio.coroutine
def hello_world():
    """
    Sends a 'hello world' message and then reads it from the queue.
    """
    # connect to the RabbitMQ broker
    connection = yield from asynqp.connect('localhost', 5672, username='guest', password='guest')

    # Open a communications channel
    channel = yield from connection.open_channel()

    # Create a queue and an exchange on the broker
    exchange = yield from channel.declare_exchange('test.exchange', 'direct')
    queue = yield from channel.declare_queue('test.queue')

    # Bind the queue to the exchange, so the queue will get messages published to the exchange
    yield from queue.bind(exchange, 'routing.key')

    # If you pass in a dict it will be automatically converted to JSON
    msg = asynqp.Message({'hello': 'world'})
    exchange.publish(msg, 'routing.key')

    # Synchronously get a message from the queue
    received_message = yield from queue.get()
    print(received_message.json())  # get JSON from incoming messages easily

    # Acknowledge a delivered message
    received_message.ack()

    yield from channel.close()
    yield from connection.close()


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(hello_world())

Reconnecting

import asyncio
import asynqp
import logging

logging.basicConfig(level=logging.INFO)

RECONNECT_BACKOFF = 1


class Consumer:

    def __init__(self, connection, queue):
        self.queue = queue
        self.connection = connection

    def __call__(self, msg):
        self.queue.put_nowait(msg)

    def on_error(self, exc):
        print("Connection lost while consuming queue", exc)


@asyncio.coroutine
def connect_and_consume(queue):
    # connect to the RabbitMQ broker
    connection = yield from asynqp.connect(
        'localhost', 5672, username='guest', password='guest')
    try:
        channel = yield from connection.open_channel()
        amqp_queue = yield from channel.declare_queue('test.queue')
        consumer = Consumer(connection, queue)
        yield from amqp_queue.consume(consumer)
    except asynqp.AMQPError as err:
        print("Could not consume on queue", err)
        yield from connection.close()
        return None
    return connection


@asyncio.coroutine
def reconnector(queue):
    try:
        connection = None
        while True:
            if connection is None or connection.is_closed():
                print("Connecting to rabbitmq...")
                try:
                    connection = yield from connect_and_consume(queue)
                except (ConnectionError, OSError):
                    print("Failed to connect to rabbitmq server. "
                          "Will retry in {} seconds".format(RECONNECT_BACKOFF))
                    connection = None
                if connection is None:
                    yield from asyncio.sleep(RECONNECT_BACKOFF)
                else:
                    print("Successfully connected and consuming test.queue")
            # poll connection state every 100ms
            yield from asyncio.sleep(0.1)
    except asyncio.CancelledError:
        if connection is not None:
            yield from connection.close()


@asyncio.coroutine
def process_msgs(queue):
    try:
        while True:
            msg = yield from queue.get()
            print("Received", msg.body)
            msg.ack()
    except asyncio.CancelledError:
        pass


def main():
    loop = asyncio.get_event_loop()
    queue = asyncio.Queue()
    # Start main indexing task in the background
    reconnect_task = loop.create_task(reconnector(queue))
    process_task = loop.create_task(process_msgs(queue))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        process_task.cancel()
        reconnect_task.cancel()
        loop.run_until_complete(process_task)
        loop.run_until_complete(reconnect_task)
    loop.close()


if __name__ == "__main__":
    main()