Guest User

Untitled

a guest
Mar 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. import time
  4.  
  5.  
  6. async def run(loop):
  7. nc = NATS()
  8.  
  9. await nc.connect(io_loop=loop)
  10.  
  11. async def message_handler(msg):
  12. subject = msg.subject
  13. reply = msg.reply
  14. data = msg.data.decode()
  15. print("Received a message on '{subject} {reply}': {data}".format(
  16. subject=subject, reply=reply, data=data))
  17.  
  18. # Working
  19. time.sleep(10)
  20.  
  21. # If nats disconnects at this point, the exception will not be caused
  22. # and will be made attempt to send a message by nc.publish
  23. await asyncio.sleep(2, loop=loop)
  24.  
  25. print("UNSLEEP")
  26. await nc.publish("test", "test payload".encode())
  27. print("PUBLISHED")
  28.  
  29. # Simple publisher and async subscriber via coroutine.
  30. await nc.subscribe("foo", cb=message_handler)
  31.  
  32. while True:
  33. await asyncio.sleep(1, loop=loop)
  34.  
  35. await nc.close()
  36.  
  37. if __name__ == '__main__':
  38. loop = asyncio.get_event_loop()
  39. loop.run_until_complete(run(loop))
  40. loop.close()
Add Comment
Please, Sign In to add comment