Skip to content

Instantly share code, notes, and snippets.

@wonderbeyond
Created August 4, 2022 15:00
Show Gist options
  • Save wonderbeyond/76a8f8bace325fb303ec2eeae6ec7684 to your computer and use it in GitHub Desktop.
Save wonderbeyond/76a8f8bace325fb303ec2eeae6ec7684 to your computer and use it in GitHub Desktop.
[signal][atexit] Demo: Handle on-exit event in Python program
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