]> granicus.if.org Git - python/commitdiff
Add settrace() and setprofile() functions to the threading library.
authorJeremy Hylton <jeremy@alum.mit.edu>
Sun, 29 Jun 2003 16:58:41 +0000 (16:58 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Sun, 29 Jun 2003 16:58:41 +0000 (16:58 +0000)
Doc/lib/libthreading.tex
Lib/threading.py

index 2fe72de91c4f34fa47466b7221c0f54c600e4a90..ad48feb5ec07f8c13ccaec83c74e02530ad330d3 100644 (file)
@@ -90,6 +90,20 @@ subclassed in a limited fashion.
 A thread that executes a function after a specified interval has passed.
 \end{classdesc*}
 
+\begin{funcdesc}{settrace}{func}
+Set a trace function \index{trace function} for all threads started
+from the \module{threading} module.  The \var{func} will be passed to 
+\cfuntion{sys.settrace} for each thread, before its \method{run}
+method is called.
+\end{funcdesc}
+
+\begin{funcdesc}{setprofile}{func}
+Set a profile function \index{profile function} for all threads started
+from the \module{threading} module.  The \var{func} will be passed to 
+\cfuntion{sys.setprofile} for each thread, before its \method{run}
+method is called.
+\end{funcdesc}
+
 Detailed interfaces for the objects are documented below.  
 
 The design of this module is loosely based on Java's threading model.
index 7ec8eff92bb391170e497a6fc30f0b377f4a2305..f48fb6e449a4ecdceee7f906d73c0b09f50b5015 100644 (file)
@@ -52,6 +52,18 @@ else:
         def _note(self, *args):
             pass
 
+# Support for profile and trace hooks
+
+_profile_hook = None
+_trace_hook = None
+
+def setprofile(func):
+    global _profile_hook
+    _profile_hook = func
+    
+def settrace(func):
+    global _trace_hook
+    _trace_hook = func
 
 # Synchronization classes
 
@@ -408,6 +420,14 @@ class Thread(_Verbose):
             _active_limbo_lock.release()
             if __debug__:
                 self._note("%s.__bootstrap(): thread started", self)
+
+            if _trace_hook:
+                self._note("%s.__bootstrap(): registering trace hook", self)
+                _sys.settrace(_trace_hook)
+            if _profile_hook:
+                self._note("%s.__bootstrap(): registering profile hook", self)
+                _sys.setprofile(_profile_hook)
+                
             try:
                 self.run()
             except SystemExit: