Better thread tracing in debug.py

This commit is contained in:
Luke Campagnola 2015-05-12 11:14:02 -04:00
parent 9df4df55c4
commit be1ed10d9a

View File

@ -1097,46 +1097,44 @@ def pretty(data, indent=''):
return ret return ret
class PeriodicTrace(object): class ThreadTrace(object):
""" """
Used to debug freezing by starting a new thread that reports on the Used to debug freezing by starting a new thread that reports on the
location of the main thread periodically. location of other threads periodically.
""" """
class ReportThread(QtCore.QThread): def __init__(self, interval=10.0):
def __init__(self): self.interval = interval
self.frame = None
self.ind = 0
self.lastInd = None
self.lock = Mutex() self.lock = Mutex()
QtCore.QThread.__init__(self) self._stop = False
self.start()
def notify(self, frame): def stop(self):
with self.lock: with self.lock:
self.frame = frame self._stop = True
self.ind += 1
def start(self, interval=None):
if interval is not None:
self.interval = interval
self._stop = False
self.thread = threading.Thread(target=self.run)
self.thread.daemon = True
self.thread.start()
def run(self): def run(self):
while True: while True:
time.sleep(1)
with self.lock: with self.lock:
if self.lastInd != self.ind: if self._stop is True:
print("== Trace %d: ==" % self.ind) return
traceback.print_stack(self.frame)
self.lastInd = self.ind
def __init__(self): print("\n============= THREAD FRAMES: ================")
self.mainThread = threading.current_thread() for id, frame in sys._current_frames().items():
self.thread = PeriodicTrace.ReportThread() if id == threading.current_thread().ident:
self.thread.start() continue
sys.settrace(self.trace) print("<< thread %d >>" % id)
traceback.print_stack(frame)
def trace(self, frame, event, arg): print("===============================================\n")
if threading.current_thread() is self.mainThread: # and 'threading' not in frame.f_code.co_filename:
self.thread.notify(frame)
# print("== Trace ==", event, arg)
# traceback.print_stack(frame)
return self.trace
time.sleep(self.interval)
class ThreadColor(object): class ThreadColor(object):