Re: [Q] Exit hook for a "C" extension module

Ken Manheimer (klm@NIST.GOV)
Wed, 15 Mar 1995 10:35:40 -0500 (EST)

On Wed, 15 Mar 1995, Raymond E. Suorsa wrote:

> [...]
> Simply put: How do I supply an exit hook for a module (i.e. how do I
> run clean up code before python exits. This is to unlink shared memory
> that will still exist even after python shuts down normally.)

Assign your exit function to sys.exitfunc - it will get called when
python exits. (This works whether you explicitly call sys.exit() to
exit, or the interpreter encounters an <eof>, or whatever. The exit func
will not get called, however, if you exit via os._exit().)

>>> def done(): print 'done'
...
>>> sys.exitfunc = done
>>> sys.exit(0)
done
%

ken
ken.manheimer@nist.gov, 301 975-3539