Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import asyncio
  2. import time
  3. import random
  4.  
  5.  
  6. async def coro(loop, idx):
  7. # distribute coros homogenity along 10 seconds
  8. # to get a homogeneous traffic.
  9. await asyncio.sleep(idx % 10)
  10. if loop.load() > 0.9:
  11. return False
  12. start = loop.time()
  13. while loop.time() - start < 0.02:
  14. pass
  15. return True
  16.  
  17. async def run(loop, n):
  18. tasks = [coro(loop, i) for i in range(n)]
  19. results = await asyncio.gather(*tasks)
  20. abandoned = len([r for r in results if not r])
  21. print("Load reached for {} coros/seq: {}, abandoned {}/{}".format(n/10, loop.load(), abandoned, n))
  22.  
  23. async def wait_load_zero():
  24. print("reseting load....")
  25. await asyncio.sleep(6)
  26.  
  27. async def main(loop):
  28. await run(loop, 100)
  29. await wait_load_zero()
  30. await run(loop, 200)
  31. await wait_load_zero()
  32. await run(loop, 400)
  33. await wait_load_zero()
  34. await run(loop, 800)
  35.  
  36. loop = asyncio.get_event_loop()
  37. loop.run_until_complete(main(loop))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement