Re: Detection of program termination

Guido.van.Rossum@cwi.nl
Fri, 16 Sep 1994 12:26:35 +0200

> I have a question about what python do to the class instances while
> program ends. I did some test and found out that the __del__ method
> was not invoked while program ends. I am curious about if there are
> some way to know the program ends within class instances.

It was the intention that all modules would be purged of their data
and then deleted (doneimport() in Python/import.c). However if you
turn on threads at compile time it will never work since doneimport()
is then not called -- there is the (remote) possibility that another
thread may still be active when the main thread exits so the purging
of all modules would be unsafe in this case.

> If there are some other ways to detect termination of program, I'd
> like to hear from you.

If you assign a function pointer to sys.exitfunc, it will be called
when the program terminates. Note that the system supports only a
single function here -- but it is trivial to implement a
"cleanup-registry" module in Python using this functionality.

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>