Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- from nats.aio.client import Client as NATS
- import time
- async def run(loop):
- nc = NATS()
- await nc.connect(io_loop=loop)
- async def message_handler(msg):
- subject = msg.subject
- reply = msg.reply
- data = msg.data.decode()
- print("Received a message on '{subject} {reply}': {data}".format(
- subject=subject, reply=reply, data=data))
- # Working
- time.sleep(10)
- # If nats disconnects at this point, the exception will not be caused
- # and will be made attempt to send a message by nc.publish
- await asyncio.sleep(2, loop=loop)
- print("UNSLEEP")
- await nc.publish("test", "test payload".encode())
- print("PUBLISHED")
- # Simple publisher and async subscriber via coroutine.
- await nc.subscribe("foo", cb=message_handler)
- while True:
- await asyncio.sleep(1, loop=loop)
- await nc.close()
- if __name__ == '__main__':
- loop = asyncio.get_event_loop()
- loop.run_until_complete(run(loop))
- loop.close()
Add Comment
Please, Sign In to add comment