[Python-Dev] Threading, atexit, and logging

"Martin v. Löwis" martin at v.loewis.de
Wed Dec 6 19:37:11 CET 2006


Tim Peters schrieb:
>> Upon investigation, it turns out that the file descriptor
>> is closed because the logging atexit handler is invoked.
>> This is surprising, as the program is far from exiting at
>> this point.
> 
> But the main thread is done, right?

Wrong. main.py (which is the __main__ script in the demo
code) is done, yes. However, threading.py has machinery
to not terminate the main thread as long as there are
non-daemon threads. In that sense, the main thread is
not done: it still has to .join() all the other threads
(rather, they join the main thread).

> Ya, and that sucks.  Can't recall details now, but it's not the first
> time the vagaries of atexit ordering bit a threaded program.  

In this case, logging/__init__.py imports threading, so that will
register its atexit first - even if the application imports logging
before threading.

> IMO, `threading` shouldn't use atexit at all.

That is (in a way) my proposal (although I suggest to use
sys.exitfunc instead).

> It's expedient :-)  So was using atexit for this to begin with.
> Probably "good enough".  I'd rather, e.g., that `threading` stuff an
> exit function into a module global, and change Py_Finalize() to look
> for that and run it (if present) before invoking call_sys_exitfunc().

Ok, that's what I'll do then.

Yet another alternative would be to have the "daemonic" thread feature
in the thread module itself (along with keeping track of a list of
all running non-daemonic thread).

Regards,
Martin


More information about the Python-Dev mailing list