summaryrefslogtreecommitdiffstats
path: root/Lib/threading.py
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2022-08-24 22:21:39 (GMT)
committerGitHub <noreply@github.com>2022-08-24 22:21:39 (GMT)
commite34c82abeb7ace09e6b5d116585c47cc372996c1 (patch)
tree16f130870af42de041ddf52a36540b9c421aec8c /Lib/threading.py
parent657976ad950e56b33b7dc15e64a0baecdd184f5a (diff)
downloadcpython-e34c82abeb7ace09e6b5d116585c47cc372996c1.zip
cpython-e34c82abeb7ace09e6b5d116585c47cc372996c1.tar.gz
cpython-e34c82abeb7ace09e6b5d116585c47cc372996c1.tar.bz2
GH-93503: Add thread-specific APIs to set profiling and tracing functions in the C-API (#93504)
* gh-93503: Add APIs to set profiling and tracing functions in all threads in the C-API * Use a separate API * Fix NEWS entry * Add locks around the loop * Document ignoring exceptions * Use the new APIs in the sys module * Update docs
Diffstat (limited to 'Lib/threading.py')
-rw-r--r--Lib/threading.py25
1 files changed, 22 insertions, 3 deletions
diff --git a/Lib/threading.py b/Lib/threading.py
index e32ad14..f28597c 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -28,7 +28,8 @@ __all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
'setprofile', 'settrace', 'local', 'stack_size',
- 'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile']
+ 'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile',
+ 'setprofile_all_threads','settrace_all_threads']
# Rename some stuff so "from threading import *" is safe
_start_new_thread = _thread.start_new_thread
@@ -60,11 +61,20 @@ def setprofile(func):
The func will be passed to sys.setprofile() for each thread, before its
run() method is called.
-
"""
global _profile_hook
_profile_hook = func
+def setprofile_all_threads(func):
+ """Set a profile function for all threads started from the threading module
+ and all Python threads that are currently executing.
+
+ The func will be passed to sys.setprofile() for each thread, before its
+ run() method is called.
+ """
+ setprofile(func)
+ _sys._setprofileallthreads(func)
+
def getprofile():
"""Get the profiler function as set by threading.setprofile()."""
return _profile_hook
@@ -74,11 +84,20 @@ def settrace(func):
The func will be passed to sys.settrace() for each thread, before its run()
method is called.
-
"""
global _trace_hook
_trace_hook = func
+def settrace_all_threads(func):
+ """Set a trace function for all threads started from the threading module
+ and all Python threads that are currently executing.
+
+ The func will be passed to sys.settrace() for each thread, before its run()
+ method is called.
+ """
+ settrace(func)
+ _sys._settraceallthreads(func)
+
def gettrace():
"""Get the trace function as set by threading.settrace()."""
return _trace_hook