Move the code into the main() routine and pass main_loop as a parameter to the signal handler.
Remove unused imports and fix import sorting using isort utility.