如何在python中找到死锁线程

时间:2018-04-09 04:35:26

标签: python django python-3.x deadlock

有谁知道如何在python(Django)程序中找到死锁线程?

我知道如何在Java中做如下(但在Python中不知道):

ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long[] threadIds = bean.findDeadlockedThreads();
if (threadIds != null) {
    ThreadInfo[] infos = bean.getThreadInfo(threadIds);
    ...

修改

我故意在我的Django2应用程序的views.py中编写以下代码。

a_lock = threading.Lock()
b_lock = threading.Lock()
switch_flag = True

def deadlock(request):
    global switch_flag
    if switch_flag:
        with a_lock:
            print("Locked a_lock.")
            switch_flag = False
            sleep(5)
            with b_lock:
                print("Locked a_lock. -> Locked b_lock.")
    else:
        with b_lock:
            print("Locked b_lock.")
            switch_flag = True
            sleep(5)
            with a_lock:
                print("Locked b_lock. -> Locked a_lock.")

版本:

$ python --version
Python 3.6.3
$ django-admin.py --version
2.0.4

0 个答案:

没有答案