Created
August 20, 2017 22:58
-
-
Save pfreixes/fd26c36391b33056b7efd525e4690aef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Author
pfreixes
commented
Aug 20, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment