Python 3.3+threadingfunction
threading.get_ident()
thread introspection
An integer identifier of the current thread inside Python — a key for a per-thread dictionary; the same as Thread.ident.
Syntax
threading.get_ident()
The number means nothing by itself and is not the OS thread number shown by top or a debugger — get_native_id() gives that one. Once a thread ends its identifier may be reused by a new one, so data keyed by get_ident() that outlives its thread passes to a stranger. threading.local() is usually easier than a dictionary keyed by ident.
Examples
import threading
main = threading.get_ident()
print(main) # → ? 140213411428160
print(main == threading.main_thread().ident) # → True
ids = []
t = threading.Thread(target=lambda: ids.append(threading.get_ident()))
t.start()
t.join()
print(ids[0] == t.ident, ids[0] != main) # → True True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy