Python 3.12+threadingfunction
threading.settrace_all_threads()
tracing and profiling
Installs a trace function in every thread at once — those already running, the current one included, and those started later (Python 3.12+).
Syntax
threading.settrace_all_threads(func)
The difference from settrace(): that one only affects future threads, while this function also calls sys.settrace in every running thread. That lets a debugger attached mid-run see threads started before it. Remove tracing everywhere with settrace_all_threads(None). Frames already executing get no per-line events: the local tracer is installed on the call event, i.e. from the next function call on.
Examples
import sys
import threading
calls = []
def tracer(frame, event, arg):
if event == 'call' and frame.f_code.co_name == 'step':
calls.append(threading.current_thread().name)
return None
def step():
pass
started, go = threading.Event(), threading.Event()
def worker():
started.set()
go.wait()
step()
t = threading.Thread(target=worker, name='old')
t.start()
started.wait()
threading.settrace_all_threads(tracer) # поток 'old' уже работает
step()
go.set()
t.join()
threading.settrace_all_threads(None)
print(sorted(calls), sys.gettrace()) # → ['MainThread', 'old'] None
See also
Open in the full glossarydocs.python.orgReport an inaccuracy