コード例 #1
0
    async def send_rabbit(self, request):

        connection = connect_robust("amqp://*****:*****@localhost:5672/")

        routing_key = "test_key"

        channel = await connection.channel()
        exchange = await channel.declare_exchange('message', durable=True)
        queue = await channel.declare_queue("test_queue")
        await queue.bind(exchange, routing_key)
        await exchange.publish(
            Message(
                bytes("Hello, World!", 'utf-8'),
                type="text",
                content_type='application/json',
                headers={'foo': 'bar'}
            ),
            routing_key
        )
        # incoming_message = await queue.get(timeout=5)
        # print(incoming_message)
        # Подтвердить получение
        # await incoming_message.ack()
        # await queue.unbind(exchange, routing_key)
        # await queue.delete()
        await connection.close()
コード例 #2
0
    def create_connection(self, cleanup=True):
        client = yield from connect_robust(AMQP_URL, loop=self.loop)

        if cleanup:
            self.addCleanup(client.close)

        return client
コード例 #3
0
import asyncio
import aio_pika


async def talk():
    while True:
        print('something')
        await asyncio.sleep(0.5)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(
        asyncio.wait([
            aio_pika.connect_robust("amqp://*****:*****@127.0.0.1", loop=loop),
            talk()
        ]))
    loop.close()