Skip to content

Instantly share code, notes, and snippets.

@mikeckennedy
Created May 27, 2020 17:34
Show Gist options
  • Save mikeckennedy/d9ac5a600f91971c6933b4f41a8df480 to your computer and use it in GitHub Desktop.
Save mikeckennedy/d9ac5a600f91971c6933b4f41a8df480 to your computer and use it in GitHub Desktop.
Demonstrates issue discussed over at https://nullprogram.com/blog/2020/05/24/
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