async def main(): conn = AsyncioAmqpConnection(AmqpParameters()) channel = conn.channel() exchange = channel.exchange('events.exchange', type='topic') queue = channel.queue('events.queue') queue.bind(exchange, 'logs.topic') queue.consume(consumer) await conn.start() while True: # wait for messages await asyncio.sleep(1)
async def main(): conn = AsyncioAmqpConnection(AmqpParameters()) channel = conn.channel() first_exchange = channel.exchange('events.first', type='topic') first_queue = channel.queue('events.first') first_queue.bind(first_exchange, 'msg') first_queue.consume(consumer) second_stage = conn.stage('2:second') second_exchange = channel.exchange( 'events.second', type='topic', stage=second_stage, ) second_queue = channel.queue( 'events.second', stage=second_stage, ) second_queue.bind( second_exchange, 'msg', stage=second_stage, ) second_queue.consume( consumer, stage=second_stage, ) await conn.start() while FIRST_COUNT < 3: # wait for messages await asyncio.sleep(.1) await conn.next_stage() while SECOND_COUNT < 3: # wait for messages await asyncio.sleep(.1) await conn.stop()
async def main(): conn = AsyncioAmqpConnection(AmqpParameters()) channel = conn.channel() channel.exchange('events.exchange', type='topic') await conn.start() while True: await asyncio.sleep(1) try: await conn.publish( channel, AmqpMsg( exchange='events.exchange', topic='logs.topic', payload=b'hello world', )) except AmqpConnectionNotOpen: print('Not connected to an AMQP server...') pass
async def main(): conn = AsyncioAmqpConnection(AmqpParameters()) channel = conn.channel() channel.exchange('events.first', type='topic') second_stage = conn.stage('2:second') channel.exchange( 'events.second', type='topic', stage=second_stage, ) await conn.start() for _ in range(3): await asyncio.sleep(1) await conn.publish( channel, AmqpMsg( exchange='events.first', topic='msg', payload=b'first', )) await conn.next_stage() for _ in range(3): await asyncio.sleep(1) await conn.publish( channel, AmqpMsg( exchange='events.second', topic='msg', payload=b'second', )) await conn.stop()
def main(): conn = GeventAmqpConnection(AmqpParameters()) channel = conn.channel() channel.exchange('events.first', type='topic') second_stage = conn.stage('2:second') channel.exchange( 'events.second', type='topic', stage=second_stage, ) conn.start() for _ in range(3): sleep(1) conn.publish( channel, AmqpMsg( exchange='events.first', topic='msg', payload=b'first', )) conn.run_stage(second_stage) for _ in range(3): sleep(1) conn.publish( channel, AmqpMsg( exchange='events.second', topic='msg', payload=b'second', )) conn.stop()
from gevent import monkey # isort:skip monkey.patch_all() # isort:skip import timeit # noqa: E402 from gevent import sleep # noqa: E402 from simple_amqp import AmqpParameters # noqa: E402 from simple_amqp_pubsub import Source # noqa: E402 from simple_amqp_pubsub.gevent import GeventAmqpPubSub # noqa: E402 pubsub_conn = GeventAmqpPubSub(params=AmqpParameters(), ) LOGS_SOURCE = Source(name='logs') logs_client = pubsub_conn.client(LOGS_SOURCE) pubsub_conn.configure() pubsub_conn.start() inc = 0 while True: start = timeit.default_timer() sleep(.001) logs_client.push('logs', 'log #{}'.format(inc)) inc += 1 end = timeit.default_timer() print('# dt {0:.2f}ms'.format((end - start) * 1000))
import timeit from asyncio import get_event_loop, set_event_loop_policy, sleep import uvloop from simple_amqp import AmqpParameters from simple_amqp_pubsub import Event, Pipe, Source, Subscriber from simple_amqp_pubsub.asyncio import AsyncioAmqpPubSub set_event_loop_policy(uvloop.EventLoopPolicy()) pubsub_conn = AsyncioAmqpPubSub(params=AmqpParameters(), ) LOGS_SOURCE = Source(name='logs') LOGS_PIPE = Pipe( name='logs.worker', retries=['5s', '10s', '30s'], ) class LogService: sub = Subscriber(LOGS_PIPE) def __init__(self): self._last_dt = timeit.default_timer() @sub.listen(LOGS_SOURCE, 'logs') async def logs(self, event: Event): log_line = event.payload print('## log line: ', log_line)
from gevent import monkey # isort:skip monkey.patch_all() # isort:skip from gevent import sleep # noqa: E402 from simple_amqp import AmqpParameters # noqa: E402 from simple_amqp_rpc import Service # noqa: E402 from simple_amqp_rpc.gevent import GeventAmqpRpc # noqa: E402 rpc_conn = GeventAmqpRpc( params=AmqpParameters(), route='ping', ) class PingService: svc = Service('ping') @svc.rpc def ping(self, name: str): return 'pong: {}'.format(name) ping_service = PingService() rpc_conn \ .add_svc(ping_service.svc, ping_service) rpc_conn.configure() rpc_conn.start()