Created
August 4, 2022 15:00
-
-
Save wonderbeyond/76a8f8bace325fb303ec2eeae6ec7684 to your computer and use it in GitHub Desktop.
[signal][atexit] Demo: Handle on-exit event in Python program
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
from typing import List | |
import os | |
import atexit | |
import signal | |
files_to_remove_on_exit: List[str] = [] | |
def on_exit(*args): | |
for filename in files_to_remove_on_exit: | |
print(f'Removing {filename}') | |
os.remove(filename) | |
signal.signal(signal.SIGINT, on_exit) | |
signal.signal(signal.SIGTERM, on_exit) | |
atexit.register(on_exit) | |
# file_to_remove_on_exit.append(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment