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 threading.MainThread , thinking that I can think of some methods, nobody really does not satisfy me.

See the name of thread

The main thread has been started in threading.py in this way:

  Thread .__ init __ (self, name = "menu")   

Any one can do this

  if threading. Current_thread (). Name == 'MainThread'   

But is this name fixed? Other code I have seen whether the

MainThread is included anywhere in the name of thread.

Store the Initial Thread

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?

problem with threading.current_thread () Name == 'MainThread' It can always do this:

  Threading.current_thread (). Name = 'MyName' threading.current_thread (). Name == 'MainThread' will fail #   

Perhaps the following is more solid:

  threading.current_thread () .__ class_________ = '_MainThread'   

After saying this, one can still manipulate:

  threading. Current_thread () .__ class __.__ Name__ = Threading Cruelty_read () .__ class_________ _ = '_MainThread' will fail # /   

but this option still looks better;

Update:

Python 3.4 which is much more beautiful than the above:

  Threading Crude_thread () == threading.main_thread ()   

Update 2:

As mentioned in the comment below, by user 1300959, another viable And the clean option is:

  isinstance (threading.current_thread (), threading._MainThread)    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -