Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import time
- import random
- async def coro(loop, idx):
- # distribute coros homogenity along 10 seconds
- # to get a homogeneous traffic.
- await asyncio.sleep(idx % 10)
- if loop.load() > 0.9:
- return False
- start = loop.time()
- while loop.time() - start < 0.02:
- pass
- return True
- async def run(loop, n):
- tasks = [coro(loop, i) for i in range(n)]
- results = await asyncio.gather(*tasks)
- abandoned = len([r for r in results if not r])
- print("Load reached for {} coros/seq: {}, abandoned {}/{}".format(n/10, loop.load(), abandoned, n))
- async def wait_load_zero():
- print("reseting load....")
- await asyncio.sleep(6)
- async def main(loop):
- await run(loop, 100)
- await wait_load_zero()
- await run(loop, 200)
- await wait_load_zero()
- await run(loop, 400)
- await wait_load_zero()
- await run(loop, 800)
- loop = asyncio.get_event_loop()
- loop.run_until_complete(main(loop))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement