From e06fc101f5ba2e1313de0f33bc9f56b91b1a4611 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Wed, 13 Sep 2017 22:13:50 -0700 Subject: [PATCH 1/2] Add function to enable faulthandler on all threads --- pyqtgraph/debug.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pyqtgraph/debug.py b/pyqtgraph/debug.py index 0da24d7c..61ae9fd5 100644 --- a/pyqtgraph/debug.py +++ b/pyqtgraph/debug.py @@ -1186,3 +1186,23 @@ class ThreadColor(object): c = (len(self.colors) % 15) + 1 self.colors[tid] = c return self.colors[tid] + + +def enableFaulthandler(): + """ Enable faulthandler for all threads. + + If the faulthandler package is available, this function disables and then + re-enables fault handling for all threads (this is necessary to ensure any + new threads are handled correctly), and returns True. + + If faulthandler is not available, then returns False. + """ + try: + import faulthandler + # necessary to disable first or else new threads may not be handled. + faulthandler.disable() + faulthandler.enable(all_threads=True) + return True + except ImportError: + return False + From 1911a26f8488689c4844c3a37acbd81eaea4696f Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Wed, 13 Sep 2017 22:14:29 -0700 Subject: [PATCH 2/2] Allow Mutex to be used as drop-in replacement for python's Lock --- pyqtgraph/util/mutex.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/pyqtgraph/util/mutex.py b/pyqtgraph/util/mutex.py index 4a193127..c03c65c4 100644 --- a/pyqtgraph/util/mutex.py +++ b/pyqtgraph/util/mutex.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- -from ..Qt import QtCore import traceback +from ..Qt import QtCore + class Mutex(QtCore.QMutex): """ @@ -17,7 +18,7 @@ class Mutex(QtCore.QMutex): QtCore.QMutex.__init__(self, *args) self.l = QtCore.QMutex() ## for serializing access to self.tb self.tb = [] - self.debug = True ## True to enable debugging functions + self.debug = kargs.pop('debug', False) ## True to enable debugging functions def tryLock(self, timeout=None, id=None): if timeout is None: @@ -72,6 +73,16 @@ class Mutex(QtCore.QMutex): finally: self.l.unlock() + def acquire(self, blocking=True): + """Mimics threading.Lock.acquire() to allow this class as a drop-in replacement. + """ + return self.tryLock() + + def release(self): + """Mimics threading.Lock.release() to allow this class as a drop-in replacement. + """ + self.unlock() + def depth(self): self.l.lock() n = len(self.tb) @@ -91,4 +102,13 @@ class Mutex(QtCore.QMutex): def __enter__(self): self.lock() - return self \ No newline at end of file + return self + + +class RecursiveMutex(Mutex): + """Mimics threading.RLock class. + """ + def __init__(self, **kwds): + kwds['recursive'] = True + Mutex.__init__(self, **kwds) +