Cannot start a thread in atexit callback

Graham Dumpleton Graham.Dumpleton at gmail.com
Wed May 6 03:26:07 EDT 2009


On May 6, 3:18 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> En Tue, 05 May 2009 23:52:25 -0300, Zac Burns <zac... at gmail.com> escribió:
>
> > It seems that one cannot start a thread in an atexit callback.
>
> > My use case is that I have a IO heavy callback that I want to run in a
> > thread so that other callbacks can finish while it's doing it's thing
> > to save on exit time.
>
> Try creating the thread when the program begins, but locked. And release  
> the lock when your programs is about to finish.

FWIW, from Python 2.5 (??) onwards, a shutdown of non daemonized
threads is performed as a separate step before atexit callbacks. Not
sure if other aspects of threading may be undone at that point and so
prevent startup of new ones.

The code in threading module which is called is:

class _MainThread(Thread):

    ...

    def _exitfunc(self):
        self._Thread__stop()
        t = _pickSomeNonDaemonThread()
        if t:
            if __debug__:
                self._note("%s: waiting for other threads", self)
        while t:
            t.join()
            t = _pickSomeNonDaemonThread()
        if __debug__:
            self._note("%s: exiting", self)
        self._Thread__delete()

_shutdown = _MainThread()._exitfunc

The call to this is done from WaitForThreadShutdown() in main.c. Which
is in turn called from Py_Main() just prior to calling Py_Finalize().

        WaitForThreadShutdown();

        Py_Finalize();

This is all different to older versions of Python which shutdown such
threads from an actual atexit handler. This caused various ordering
issues.

Anyway, the point of this is that it would need to be a daemonized
thread to begin with. :-)

Graham



More information about the Python-list mailing list