File: emit_log.py

package info (click to toggle)
python-aioamqp 0.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 456 kB
  • sloc: python: 2,741; makefile: 187
file content (35 lines) | stat: -rwxr-xr-x 833 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
"""
    RabbitMQ.com pub/sub example

    https://www.rabbitmq.com/tutorials/tutorial-three-python.html

"""

import asyncio
import aioamqp

import sys


async def exchange_routing():
    try:
        transport, protocol = await aioamqp.connect('localhost', 5672)
    except aioamqp.AmqpClosedConnection:
        print("closed connections")
        return

    channel = await protocol.channel()
    exchange_name = 'logs'
    message = ' '.join(sys.argv[1:]) or "info: Hello World!"

    await channel.exchange_declare(exchange_name=exchange_name, type_name='fanout')

    await channel.basic_publish(message, exchange_name=exchange_name, routing_key='')
    print(" [x] Sent %r" % (message,))

    await protocol.close()
    transport.close()


asyncio.get_event_loop().run_until_complete(exchange_routing())