11from python25 cimport PyFrameObject, PyObject, PyStringObject
22
3+ import threading
34
45cdef extern from " frameobject.h" :
56 ctypedef int (* Py_tracefunc)(object self , PyFrameObject * py_frame, int what, PyObject * arg)
@@ -108,14 +109,14 @@ cdef class LineProfiler:
108109 cdef public dict code_map
109110 cdef public dict last_time
110111 cdef public double timer_unit
111- cdef public long enable_count
112+ cdef public object threaddata
112113
113114 def __init__ (self , *functions ):
114115 self .functions = []
115116 self .code_map = {}
116117 self .last_time = {}
117118 self .timer_unit = hpTimerUnit()
118- self .enable_count = 0
119+ self .threaddata = threading.local()
119120 for func in functions:
120121 self .add_function(func)
121122
@@ -135,17 +136,19 @@ cdef class LineProfiler:
135136 def enable_by_count (self ):
136137 """ Enable the profiler if it hasn't been enabled before.
137138 """
138- if self .enable_count == 0 :
139+ if not hasattr (self .threaddata, ' enable_count' ):
140+ self .threaddata.enable_count = 0
141+ if self .threaddata.enable_count == 0 :
139142 self .enable()
140- self .enable_count += 1
143+ self .threaddata. enable_count += 1
141144
142145 def disable_by_count (self ):
143146 """ Disable the profiler if the number of disable requests matches the
144147 number of enable requests.
145148 """
146- if self .enable_count > 0 :
147- self .enable_count -= 1
148- if self .enable_count == 0 :
149+ if self .threaddata. enable_count > 0 :
150+ self .threaddata. enable_count -= 1
151+ if self .threaddata. enable_count == 0 :
149152 self .disable()
150153
151154 def __enter__ (self ):
0 commit comments