Skip to content

Instantly share code, notes, and snippets.

@pfreixes
Created August 20, 2017 22:58
Show Gist options
  • Save pfreixes/fd26c36391b33056b7efd525e4690aef to your computer and use it in GitHub Desktop.
Save pfreixes/fd26c36391b33056b7efd525e4690aef to your computer and use it in GitHub Desktop.
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))
@pfreixes
Copy link
Author

$ ./python.exe ../test_load2.py
Load reached for 10.0 coros/seq: 0.20594804872227002, abandoned 0/100
reseting load....
Load reached for 20.0 coros/seq: 0.40599215789994814, abandoned 0/200
reseting load....
Load reached for 40.0 coros/seq: 0.8055964270483202, abandoned 0/400
reseting load....
Load reached for 80.0 coros/seq: 0.9390106831339007, abandoned 450/800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment