nats_produce_async.py 824 B

123456789101112131415161718192021222324252627282930313233343536
  1. import asyncio
  2. import nats
  3. from time import sleep
  4. from data_develop import develop_random_data
  5. from config import (
  6. nats_url,
  7. nats_subject,
  8. index_end,
  9. index_begin
  10. )
  11. async def develop_publish(nc:object):
  12. """
  13. Function which is about the development of the
  14. Input: nc = nats connection
  15. Output: we developed the publish
  16. """
  17. for i in range(index_begin, index_end):
  18. await nc.publish(
  19. nats_subject,
  20. develop_random_data(i).encode()
  21. )
  22. sleep(0.02)
  23. print(f'Sent Index: ', i)
  24. print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
  25. async def main():
  26. nc = await nats.connect(nats_url)
  27. await develop_publish(nc)
  28. await nc.flush()
  29. await nc.close()
  30. if __name__ == '__main__':
  31. asyncio.run(main())