Created
May 27, 2020 17:34
-
-
Save mikeckennedy/d9ac5a600f91971c6933b4f41a8df480 to your computer and use it in GitHub Desktop.
Demonstrates issue discussed over at https://nullprogram.com/blog/2020/05/24/
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 | |
async def heartbeat(): | |
while True: | |
start = time.time() | |
await asyncio.sleep(1) | |
delay = time.time() - start - 1 | |
print(f'heartbeat delay = {delay:.3f}s') | |
JOB_DURATION = 0.01 # 10ms | |
async def process(): | |
time.sleep(JOB_DURATION) # simulate CPU time | |
JOB_COUNT = 200 | |
async def main(): | |
asyncio.create_task(heartbeat()) | |
await asyncio.sleep(2.5) | |
print('begin processing') | |
count = JOB_COUNT | |
for _ in range(JOB_COUNT): | |
asyncio.create_task(process()) | |
await asyncio.sleep(5) | |
if __name__ == '__main__': | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment