Created
May 27, 2020 17:35
-
-
Save mikeckennedy/f23b9b5abd9452cdc8b3bacaf1c3da20 to your computer and use it in GitHub Desktop.
Fixes issue discussed over at https://nullprogram.com/blog/2020/05/24/
This file contains 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 | |
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