Signal handling is not done in the main thread, so calling sys.exit()
won't do the trick of stopping the main thread. Actually, calling this will only raise a special internal exception (SystemExit
) which will terminate the current thread, if uncaught. There is no python-internal standard way of signaling one thread to please terminate, so ending the signal-handling thread won't terminate the main thread.
You have several options.
One is to check inside the longrunner thread for a certain condition (global variable or threading.Condition
or similar), and if this states to "please terminate", then stop looping.
Another is to use the operating system's facilities and kill your process with all its threads (e. g. by sending a signal you don't catch, e. g. SIGKILL, to yourself).