multithreading - Check if current thread is main thread, in Python -
The first answer is given for this, but apparently not for Python. How do I reliably determine that the current thread is the main thread? Thinking as simple as comparison of The main thread has been started in Any one can do this But is this name fixed? Other code I have seen whether the I can store the reference of the initial thread. At this time the program starts, i.e. there are no other threads yet. It would be absolutely reliable, but it is very difficult for such a simple query? Is there a more concise way to do this? Perhaps the following is more solid: After saying this, one can still manipulate: but this option still looks better; Update: Python 3.4 which is much more beautiful than the above: Update 2: As mentioned in the comment below, by user 1300959, another viable And the clean option is: threading.MainThread , thinking that I can think of some methods, nobody really does not satisfy me.
See the name of thread
threading.py in this way:
Thread .__ init __ (self, name = "menu")
if threading. Current_thread (). Name == 'MainThread'
Store the Initial Thread
problem with threading.current_thread () Name == 'MainThread' It can always do this:
Threading.current_thread (). Name = 'MyName' threading.current_thread (). Name == 'MainThread' will fail #
threading.current_thread () .__ class_________ = '_MainThread'
threading. Current_thread () .__ class __.__ Name__ = Threading Cruelty_read () .__ class_________ _ = '_MainThread' will fail # /
Threading Crude_thread () == threading.main_thread ()
isinstance (threading.current_thread (), threading._MainThread)
Comments
Post a Comment