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.lock = Mutex()
self.ind = 0 self._stop = False
self.lastInd = None self.start()
self.lock = Mutex()
QtCore.QThread.__init__(self)
def notify(self, frame): def stop(self):
with self.lock: with self.lock:
self.frame = frame self._stop = True
self.ind += 1
def run(self): def start(self, interval=None):
while True: if interval is not None:
time.sleep(1) self.interval = interval
with self.lock: self._stop = False
if self.lastInd != self.ind: self.thread = threading.Thread(target=self.run)
print("== Trace %d: ==" % self.ind) self.thread.daemon = True
traceback.print_stack(self.frame)
self.lastInd = self.ind
def __init__(self):
self.mainThread = threading.current_thread()
self.thread = PeriodicTrace.ReportThread()
self.thread.start() self.thread.start()
sys.settrace(self.trace)
def trace(self, frame, event, arg): def run(self):
if threading.current_thread() is self.mainThread: # and 'threading' not in frame.f_code.co_filename: while True:
self.thread.notify(frame) with self.lock:
# print("== Trace ==", event, arg) if self._stop is True:
# traceback.print_stack(frame) return
return self.trace
print("\n============= THREAD FRAMES: ================")
for id, frame in sys._current_frames().items():
if id == threading.current_thread().ident:
continue
print("<< thread %d >>" % id)
traceback.print_stack(frame)
print("===============================================\n")
time.sleep(self.interval)
class ThreadColor(object): class ThreadColor(object):