sub_pending_limits.py 724 B

1234567891011121314151617181920212223242526272829303132333435
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin slow_pending_limits]
  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. # Set limits of 1000 messages or 5MB
  12. await nc.subscribe("updates", cb=cb, pending_bytes_limit=5*1024*1024, pending_msgs_limit=1000)
  13. # [end slow_pending_limits]
  14. await nc.publish("updates", b'All is Well')
  15. await nc.flush()
  16. # Wait for message to come in
  17. msg = await asyncio.wait_for(future, 1)
  18. print(msg)
  19. await nc.close()
  20. loop = asyncio.get_event_loop()
  21. loop.run_until_complete(example())
  22. loop.close()