Check if current thread is main thread, in Python

后端 未结 3 844
青春惊慌失措
青春惊慌失措 2020-12-08 13:00

This has been answered for Android, Objective C and C++ before, but apparently not for Python. How do I reliably determine whether the current thread is the main thread? I c

相关标签:
3条回答
  • 2020-12-08 13:46

    The problem with threading.current_thread().name == 'MainThread' is that one can always do:

    threading.current_thread().name = 'MyName'
    assert threading.current_thread().name == 'MainThread' # will fail
    

    Perhaps the following is more solid:

    threading.current_thread().__class__.__name__ == '_MainThread'
    

    Having said that, one may still cunningly do:

    threading.current_thread().__class__.__name__ = 'Grrrr'
    assert threading.current_thread().__class__.__name__ == '_MainThread' # will fail
    

    But this option still seems better; "after all, we're all consenting adults here."

    UPDATE:

    Python 3.4 introduced threading.main_thread() which is much better than the above:

    assert threading.current_thread() is threading.main_thread()
    

    UPDATE 2:

    For Python < 3.4, perhaps the best option is:

    isinstance(threading.current_thread(), threading._MainThread)
    
    0 讨论(0)
  • 2020-12-08 13:55

    The answers here are old and/or bad, so here's a current solution:

    if threading.current_thread() is threading.main_thread():
        ...
    

    This method is available since Python 3.4+.

    0 讨论(0)
  • 2020-12-08 14:07

    If, like me, accessing protected attributes gives you the Heebie-jeebies, you may want an alternative for using threading._MainThread, as suggested. In that case, you may exploit the fact that only the Main Thread can handle signals, so the following can do the job:

    import signal
    def is_main_thread():
        try:
            # Backup the current signal handler
            back_up = signal.signal(signal.SIGINT, signal.SIG_DFL)
        except ValueError:
            # Only Main Thread can handle signals
            return False
        # Restore signal handler
        signal.signal(signal.SIGINT, back_up)
        return True
    

    Updated to address potential issue as pointed out by @user4815162342.

    0 讨论(0)
提交回复
热议问题