Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 60 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  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()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top