Title: document behavior of calling atexit.register() while atexit._run_exitfuncs is running
Type: Stage:
Components: Documentation Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, martin.panter, skip.montanaro
Priority: low Keywords:

Created on 2014-11-13 20:52 by skip.montanaro, last changed 2015-07-21 07:43 by ethan.furman.

Messages (2)
msg231135 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2014-11-13 20:52
A discussion on comp.lang.python about prettying up the "if __name__ == 'main__'" idiom led to a suggestion that a decorator could simple register the main function using atexit.register. That looks like it will work, but leaves open the possibility that while main() is running via atexit._run_exitfuncs, other exit functions might be registered.

As currently defined (at least in the Python version with 2.7), I think everything will work fine. Still, the behavior of adding new exit functions during exit is not defined. Would be kind of nice if this behavior was blessed, and then mentioned in the documentation.
msg231354 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2014-11-19 05:08
From a post by Ian Kelly (
In fact it seems the behavior does differ between Python 2.7 and Python 3.4:

$ cat
import atexit

def main():

def goodbye():
$ python2
$ python3
Date User Action Args
2015-07-21 07:43:20ethan.furmansetnosy: - ethan.furman
2014-11-19 05:08:20ethan.furmansetmessages: + msg231354
2014-11-14 22:18:07martin.pantersetnosy: + martin.panter
2014-11-13 20:57:55ethan.furmansetnosy: + ethan.furman
2014-11-13 20:52:37skip.montanarocreate