Having the following hello.py :
import time
import sys
def do_something_long():
for _ in range(30):
print(".", end="")
sys.stdout.flush()
time.sleep(1)
class Something:
def __init__(self):
pass
def __enter__(self):
print("Something __enter__ called")
return self
def __exit__(self, typ, value, traceback):
print("Something __exit__ called")
if __name__ == "__main__":
with Something():
do_something_long()
I'm very surprised not to see "Something.__exit__"
when I send a SIGTERM to the running process.
I've seen https://stackoverflow.com/a/40866947/446302 as the way to catch signals, but __exit__
method isn't guaranteed to be called to do necessary clean-up ?
According to https://en.wikipedia.org/wiki/Signal_(IPC)#SIGTERM , SIGTERM is the appropriate signal to ask the program to do a clean end of process.