I have Python program that runs multiple asyncio tasks using asyncio.TaskGroup.
All of the tasks are waiting for some event to happen and I want them being cancelled when any of them completes.
I want that the program can also be closed by sending an interrupt signal (Ctrl+C in terminal), but to prevent it being closed accidentally, that should succeed only when it is sent two times within two seconds.
try-except
blocks can be used around asyncio.run
to catch KeyboardInterrupt
, but that closes all the tasks and I would need to recreate them if interrupt happened only once and I want to continue the tasks.
I found a solution already that seems to work, but I am not 100% sure if it's the correct way.