Assume we have the following script:
def sync_sleep(x):
print("sync_sleep going to sleep")
time.sleep(x)
print("sync_sleep awake")
async def main():
loop = asyncio.get_running_loop()
loop.run_in_executor(None, sync_sleep, 4)
print("main going to sleep")
await asyncio.sleep(1)
print("main awake ")
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print("main finished")
Note: I have omitted loop.close
intentionally.
Now when I execute the above script:
python script.py
I have the following output:
sync_sleep going to sleep
main going to sleep
main awake
main finished
sync_sleep awake
I expected after running the script, the process to exit after printing "main finished"
, but it did not until the job sync_sleep
has finished. I mean, if I wanted to wait for that job, I would have added the line:
loop.run_until_complete(loop.shutdown_default_executor())
Is my expectation wrong?