是否可以在 Python 中对 Lock() 对象进行子类化?如果没有,还有其他调试死锁的方法吗? [英] Is it possible to subclass Lock() objects in Python? If not, other ways to debug deadlock?

查看:22
本文介绍了是否可以在 Python 中对 Lock() 对象进行子类化?如果没有,还有其他调试死锁的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个多线程 python 程序,它目前正处于死锁状态.我打算通过子类化 threading.Lock 对象来记录锁定获取:

导入回溯类 DebugLock(threading.Lock):def获取(自我):打印 >>sys.stderr, 获得", 自我#traceback.print_tbthreading.Lock.acquire(self)定义释放(自我):打印 >>sys.stderr, 已发布", 自我#traceback.print_tbthreading.Lock.release(self)

当我尝试运行该程序时,出现以下错误:

 class DebugLock(threading.Lock):TypeError:调用元类基类时出错无法创建builtin_function_or_method"实例

所以,我的问题是双重的:

  1. 是否可以将 Lock 对象子类化来做我正在做的事情?

  2. 如果没有,在 python 中调试死锁的最佳方法是什么?

注意:我没有写任何 Python 扩展.还有一个类似的问题:How to debug deadlock with python?但是,它涉及编译 C++ 代码和使用 GDB,因为我的代码是纯 python 的,所以我不能这样做.

解决方案

你可以使用has a lock"与是一把锁"方法,像这样:

导入线程、回溯、系统类调试锁(对象):def __init__(self):self._lock = threading.Lock()def获取(自我):打印(获得",自我)#traceback.print_tbself._lock.acquire()定义释放(自我):打印(发布",自我)#traceback.print_tbself._lock.release()def __enter__(self):self.acquire()def __exit__(self, type, value, traceback):self.release()

因为您可能希望将 with 语法与您的锁一起使用(谁不会?),所以我在其中抛出了适当的上下文保护.

用法如下所示:

<前>>>> 锁 = DebugLock()>>> 带锁:...打印(我是原子的!")...在 0x7f8590e50190 处获得了 <__main__.DebugLock 对象>我是原子的!在 0x7f8590e50190 处发布了 <__main__.DebugLock 对象>>>>

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:

  1. Is it possible to subclass Lock objects to do what I'm doing?

  2. 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:

import threading, traceback, sys
class DebugLock(object):
    def __init__(self):
        self._lock = threading.Lock()
    def acquire(self):
        print("acquired", self)
        #traceback.print_tb
        self._lock.acquire()
    def release(self):
        print("released", self)
        #traceback.print_tb
        self._lock.release()
    def __enter__(self):
        self.acquire()
    def __exit__(self, type, value, traceback):
        self.release()

where I've thrown in the appropriate context guards since you likely want to use the with syntax 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>
    >>>

这篇关于是否可以在 Python 中对 Lock() 对象进行子类化?如果没有,还有其他调试死锁的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆