Python Glossary Русский

Python 3.12+threadingfunction

threading.setprofile_all_threads()

tracing and profiling

Installs a profiling function in every thread at once — those already running, the current one included, and those started later (Python 3.12+).

Syntax

threading.setprofile_all_threads(func)

The difference from setprofile(): that one only affects future threads, while this function also calls sys.setprofile in every running thread of the interpreter. To remove the profiler everywhere call setprofile_all_threads(None). Before 3.12 it does not exist, and you have to combine threading.setprofile() with sys.setprofile() inside each thread.

Examples

import sys
import threading
def prof(frame, event, arg):
    pass
started, go, seen = threading.Event(), threading.Event(), []
def worker():
    started.set()
    go.wait()
    seen.append(sys.getprofile() is prof)
t = threading.Thread(target=worker)
t.start()
started.wait()
threading.setprofile_all_threads(prof)  # поток уже работает
print(sys.getprofile() is prof)  # → True
go.set()
t.join()
threading.setprofile_all_threads(None)
print(seen, sys.getprofile())  # → [True] None

See also

Open in the full glossarydocs.python.orgReport an inaccuracy