unsubscribe.py 630 B

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