Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfccb35

Browse files
committed
Add settrace() and setprofile() functions to the threading library.
1 parent c98ccfd commit bfccb35

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Doc/lib/libthreading.tex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ \section{\module{threading} ---
9090
A thread that executes a function after a specified interval has passed.
9191
\end{classdesc*}
9292

93+
\begin{funcdesc}{settrace}{func}
94+
Set a trace function \index{trace function} for all threads started
95+
from the \module{threading} module. The \var{func} will be passed to
96+
\cfuntion{sys.settrace} for each thread, before its \method{run}
97+
method is called.
98+
\end{funcdesc}
99+
100+
\begin{funcdesc}{setprofile}{func}
101+
Set a profile function \index{profile function} for all threads started
102+
from the \module{threading} module. The \var{func} will be passed to
103+
\cfuntion{sys.setprofile} for each thread, before its \method{run}
104+
method is called.
105+
\end{funcdesc}
106+
93107
Detailed interfaces for the objects are documented below.
94108

95109
The design of this module is loosely based on Java's threading model.

Lib/threading.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ def __init__(self, verbose=None):
5252
def _note(self, *args):
5353
pass
5454

55+
# Support for profile and trace hooks
56+
57+
_profile_hook = None
58+
_trace_hook = None
59+
60+
def setprofile(func):
61+
global _profile_hook
62+
_profile_hook = func
63+
64+
def settrace(func):
65+
global _trace_hook
66+
_trace_hook = func
5567

5668
# Synchronization classes
5769

@@ -408,6 +420,14 @@ def __bootstrap(self):
408420
_active_limbo_lock.release()
409421
if __debug__:
410422
self._note("%s.__bootstrap(): thread started", self)
423+
424+
if _trace_hook:
425+
self._note("%s.__bootstrap(): registering trace hook", self)
426+
_sys.settrace(_trace_hook)
427+
if _profile_hook:
428+
self._note("%s.__bootstrap(): registering profile hook", self)
429+
_sys.setprofile(_profile_hook)
430+
411431
try:
412432
self.run()
413433
except SystemExit:

0 commit comments

Comments
 (0)