subscribe_queue.py 586 B

1234567891011121314151617181920212223242526272829
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin subscribe_queue]
  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. await nc.subscribe("updates", queue="workers", cb=cb)
  12. await nc.publish("updates", b'All is Well')
  13. msg = await asyncio.wait_for(future, 1)
  14. print("Msg", msg)
  15. # [end subscribe_queue]
  16. await nc.close()
  17. loop = asyncio.get_event_loop()
  18. loop.run_until_complete(example())
  19. loop.close()