mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
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
This commit is contained in:
parent
657976ad95
commit
e34c82abeb
10 changed files with 271 additions and 4 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue