[Python-ideas] atexit.register_w_signals()
Chris Angelico
rosuav at gmail.com
Sat Feb 13 08:22:07 EST 2016
On Sat, Feb 13, 2016 at 11:53 PM, Giampaolo Rodola' <g.rodola at gmail.com> wrote:
> In this case such a kind of app wouldn't use something like
> register_exit_fun(). The problems I'm trying to address are 2: run the exit
> function for the most used/obvious/portable termination signal (SIGTERM),
> which is what atexit lacks, and at the same time avoid to overwrite any
> existing signal.signal() handler. In order to do this right now I'm supposed
> to use `atexit.register` *and* a wrapper around signal.signal().
import signal
class SigTerm(SystemExit): pass
def sigterm(sig,frm): raise SigTerm
signal.signal(15,sigterm)
import atexit
atexit.register(print,"atexit called")
import os
print("I am",os.getpid())
print("Hit enter to exit.")
input()
Once you turn the signal into an exception, atexit and finally both
work as normal. Add a bit more sophistication to the first four lines,
wrap 'em up in a library function, and then you don't have to worry
about the complexity of a still-not-guaranteed atexit.
ChrisA
More information about the Python-ideas
mailing list