async def receive_log(): try: async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() exchange_name = 'direct_logs' await channel.exchange(exchange_name, 'direct') result = await channel.queue(queue_name='', durable=False, auto_delete=True) queue_name = result['queue'] severities = sys.argv[1:] if not severities: print("Usage: %s [info] [warning] [error]" % (sys.argv[0], )) sys.exit(1) for severity in severities: await channel.queue_bind( exchange_name='direct_logs', queue_name=queue_name, routing_key=severity, ) print(' [*] Waiting for logs. To exit press CTRL+C') async with anyio.fail_after(10): await channel.basic_consume(callback, queue_name=queue_name) except async_amqp.AmqpClosedConnection: print("closed connections") return
async def receive_log(): try: async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() exchange_name = 'logs' await channel.exchange(exchange_name=exchange_name, type_name='fanout') # let RabbitMQ generate a random queue name result = await channel.queue(queue_name='', exclusive=True) queue_name = result['queue'] await channel.queue_bind(exchange_name=exchange_name, queue_name=queue_name, routing_key='') print(' [*] Waiting for logs. To exit press CTRL+C') await channel.basic_consume(callback, queue_name=queue_name, no_ack=True) while True: await anyio.sleep(99999) except async_amqp.AmqpClosedConnection: print("closed connections") return
async def receive_log(): try: async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() exchange_name = 'topic_logs' await channel.exchange(exchange_name, 'topic') result = await channel.queue(queue_name='', durable=False, auto_delete=True) queue_name = result['queue'] binding_keys = sys.argv[1:] if not binding_keys: print("Usage: %s [binding_key]..." % (sys.argv[0], )) sys.exit(1) for binding_key in binding_keys: await channel.queue_bind(exchange_name='topic_logs', queue_name=queue_name, routing_key=binding_key) print(' [*] Waiting for logs. To exit press CTRL+C') await channel.basic_consume(callback, queue_name=queue_name) while True: await anyio.sleep(99999) except async_amqp.AmqpClosedConnection: print("closed connections") return
async def send(): async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() await channel.queue_declare(queue_name='hello') await channel.basic_publish(payload='Hello World!', exchange_name='', routing_key='hello') print(" [x] Sent 'Hello World!'")
async def worker(): async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() await channel.queue(queue_name='task_queue', durable=True) await channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False) await channel.basic_consume(callback, queue_name='task_queue') while True: await anyio.sleep(99999)
async def rpc_server(): async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() await channel.queue_declare(queue_name='rpc_queue') await channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False) await channel.basic_consume(on_request, queue_name='rpc_queue') print(" [x] Awaiting RPC requests") while True: await anyio.sleep(99999)
async def send(): async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() await protocol.taskgroup.spawn(get_returns, channel) await channel.queue_declare(queue_name='hello') await channel.basic_publish( payload='Hello World!', exchange_name='', routing_key='helo', # typo on purpose, will cause the return mandatory=True, ) print(" [x] Sent 'Hello World!'")
async def receive(): try: async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() await channel.queue_declare(queue_name='hello') await channel.basic_consume(callback, queue_name='hello') while True: await anyio.sleep(99999) except async_amqp.AmqpClosedConnection: print("closed connections") return
async def exchange_routing(): try: async with async_amqp.connect_amqp() as protocol: 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, )) except async_amqp.AmqpClosedConnection: print("closed connections") return
async def call(self, n): async with async_amqp.connect_amqp() as protocol: async with protocol.channel() as channel: await self.connect(channel) self.response = None self.corr_id = str(uuid.uuid4()) await self.channel.basic_publish( payload=str(n), exchange_name='', routing_key='rpc_queue', properties={ 'reply_to': self.callback_queue, 'correlation_id': self.corr_id, }, ) await self.waiter.wait() return int(self.response)
async def exchange_routing_topic(): try: async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() exchange_name = 'topic_logs' message = ' '.join(sys.argv[2:]) or 'Hello World!' routing_key = sys.argv[1] if len( sys.argv) > 1 else 'anonymous.info' await channel.exchange(exchange_name, 'topic') await channel.publish(message, exchange_name=exchange_name, routing_key=routing_key) print(" [x] Sent %r" % message) except async_amqp.AmqpClosedConnection: print("closed connections") return
async def exchange_routing(): try: async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() exchange_name = 'direct_logs' severity = sys.argv[1] if len(sys.argv) > 1 else 'info' message = ' '.join(sys.argv[2:]) or 'Hello World!' await channel.exchange(exchange_name, 'direct') await channel.publish(message, exchange_name=exchange_name, routing_key=severity) print(" [x] Sent %r" % (message, )) await protocol.aclose() transport.close() except async_amqp.AmqpClosedConnection: print("closed connections") return
async def new_task(): try: async with async_amqp.connect_amqp() as protocol: channel = await protocol.channel() await channel.queue('task_queue', durable=True) message = ' '.join(sys.argv[1:]) or "Hello World!" await channel.basic_publish( payload=message, exchange_name='', routing_key='task_queue', properties={ 'delivery_mode': 2, }, ) print(" [x] Sent %r" % message, ) except async_amqp.AmqpClosedConnection: print("closed connections") return
def connect(*a, **kw): return connect_amqp(*a, protocol=ProxyAmqpProtocol, **kw)