Python Glossary Русский

all Python 3threadingfunction

threading.current_thread()

thread introspection

The Thread object of the calling thread: find the name, ident or daemon flag of the thread the code is running in right now.

Syntax

threading.current_thread()

From the main thread it returns the thread named MainThread, from a worker its own Thread, even when the call is buried inside someone else's function. A thread started outside threading (via _thread or from a C library) gets a dummy object named Dummy-N: it cannot be join()ed, and up to 3.12 it stayed in enumerate() for good; since 3.13 it disappears from there once the alien thread exits. For logs, the %(threadName)s format field is simpler. currentThread() is a deprecated alias.

Examples

import threading
print(threading.current_thread().name)  # → MainThread
names = []
def work():
    names.append(threading.current_thread().name)
t = threading.Thread(target=work, name='loader')
t.start()
t.join()
print(names)  # → ['loader']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy