Python Glossary Русский

Python 3.4+threadingfunction

threading.main_thread()

thread introspection

The Thread object of the main thread — the one the interpreter started in; lets any code find out whether the current thread is the main one.

Syntax

threading.main_thread()

Check current_thread() is main_thread() rather than the name 'MainThread': a thread name can be changed. It matters because signal.signal() is only allowed in the main thread (ValueError otherwise), and many GUI toolkits require windows to be handled only from it. After os.fork() the thread that called fork becomes the main thread of the child process.

Examples

import threading
main = threading.main_thread()
print(main.name, threading.current_thread() is main)  # → MainThread True
res = []
def check():
    res.append(threading.current_thread() is threading.main_thread())
t = threading.Thread(target=check)
t.start()
t.join()
print(res)  # → [False]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy