I have a program which executes some long-running, synchronous tasks and may need to exit even when some of those tasks haven't finished yet. The following is a simple program which start such a task in main()
. main()
immediately returns after that and thus the program should exit.
import asyncio, time
def synchronous_task():
for i in range(5):
print(i)
time.sleep(1)
async def main():
loop.run_in_executor(None, synchronous_task)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('Main thread exiting.')
But instead I get this when running the script:
$ python3.5 test3.py
0
Main thread exiting.
1
2
3
4
What is the intended way to handle such a situation? The event loop's default executor already uses daemon threads and terminating the program without cleanup would be OK in my case, but I don't want to use os._exit()
.