unsubscribe_auto.py 579 B

1234567891011121314151617181920212223242526272829
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin unsubscribe_auto]
  5. nc = NATS()
  6. await nc.connect(servers=["nats://demo.nats.io:4222"])
  7. async def cb(msg):
  8. print(msg)
  9. sid = await nc.subscribe("updates", cb=cb)
  10. await nc.auto_unsubscribe(sid, 1)
  11. await nc.publish("updates", b'All is Well')
  12. # Won't be received...
  13. await nc.publish("updates", b'...')
  14. # [end unsubscribe_auto]
  15. await asyncio.sleep(1)
  16. await nc.close()
  17. loop = asyncio.get_event_loop()
  18. loop.run_until_complete(example())
  19. loop.close()