Skip to content

Instantly share code, notes, and snippets.

@mikeckennedy
Created May 27, 2020 17:35
Show Gist options
  • Save mikeckennedy/f23b9b5abd9452cdc8b3bacaf1c3da20 to your computer and use it in GitHub Desktop.
Save mikeckennedy/f23b9b5abd9452cdc8b3bacaf1c3da20 to your computer and use it in GitHub Desktop.
Fixes issue discussed over at https://nullprogram.com/blog/2020/05/24/
import asyncio
import time
from unsync import unsync
@unsync
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
@unsync()
def process():
time.sleep(JOB_DURATION) # simulate CPU time
return True
JOB_COUNT = 200
@unsync
async def main():
# noinspection PyAsyncCall
heartbeat()
await asyncio.sleep(2.5)
print('begin processing')
count = JOB_COUNT
tasks = [process() for _ in range(count)]
for t in tasks:
await t
await asyncio.sleep(5)
if __name__ == '__main__':
main().result()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment