[Python-Dev] Add function to signal module for getting main thread id (original) (raw)

Victor Stinner victor.stinner at gmail.com
Fri Aug 30 11:36:57 CEST 2013


2013/8/30 Andrew Svetlov <andrew.svetlov at gmail.com>:

Tulip uses check like threading.currentthread().name == 'MainThread'

You should use the identifier, not the name: threading.current_thread().ident.

This approach has a problem: thread name is writable attribute and can be changed by user code.

The ident attribute cannot be modified.

My proposition is to add function like getmainthreadid() -> int which return ident for main thread (I know function name is not perfect, please guess better one).

Just call threading.get_ident() at startup, when you have only one thread (the main thread).

There is an ugly hack to get the identifier of the main thread: threading._shutdown.self.ident.

Signal module already has required data as internal variable static long mainthread; I just guess to expose this value to python. Thoughts?

If we expose the identifier of the main thread, something should be added to the threading module, not the signal module.

Is it possible that the main thread exit while there are still other live threads?

Victor



More information about the Python-Dev mailing list