So, I’ve got a multithreaded python program, which is currently suffering from deadlock. I was going to log lock acquiring by subclassing threading.Lock objects:
import traceback
class DebugLock(threading.Lock):
def acquire(self):
print >>sys.stderr, "acquired", self
#traceback.print_tb
threading.Lock.acquire(self)
def release(self):
print >>sys.stderr, "released", self
#traceback.print_tb
threading.Lock.release(self)
When I try to run the program, I get the following error:
class DebugLock(threading.Lock):
TypeError: Error when calling the metaclass bases
cannot create 'builtin_function_or_method' instances
So, my question is twofold:
-
Is it possible to subclass Lock objects to do what I’m doing?
-
If not, what is the best way to debug deadlock in python?
Note: I’m not writing any Python extension. There’s a similar question: How to debug deadlock with python?
However, it deals with compiling C++ code and using GDB, which I can’t do since my code is pure python.
You could just use the "has a lock" versus "is a lock" approach, like so:
where I’ve thrown in the appropriate context guards since you likely want to use the
withsyntax with your locks (who wouldn’t?).Usage shown below:
>>> lock = DebugLock() >>> with lock: ... print("I'm atomic!") ... acquired <__main__.DebugLock object at 0x7f8590e50190> I'm atomic! released <__main__.DebugLock object at 0x7f8590e50190> >>>