ping_5.py 626 B

123456789101112131415161718192021222324252627282930
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin ping_5]
  5. nc = NATS()
  6. await nc.connect(
  7. servers=["nats://demo.nats.io:4222"],
  8. # Set maximum number of PINGs out without getting a PONG back
  9. # before the connection will be disconnected as a stale connection.
  10. max_outstanding_pings=5,
  11. ping_interval=1,
  12. )
  13. # Do something with the connection.
  14. # [end ping_5]
  15. while True:
  16. if nc.is_closed:
  17. break
  18. await asyncio.sleep(1)
  19. await nc.close()
  20. loop = asyncio.get_event_loop()
  21. loop.run_until_complete(example())
  22. loop.close()