Python中的跟踪子进程和线程

问题描述 投票:0回答:1

我有一个在Linux(Ubuntu 18.04)中运行的Python脚本,该脚本使用multiprocessing库创建进程,并为它们分配要执行的功能。问题是这些功能之一卡住了,我想知道它发生在哪里。我尝试使用python -m trace -l myscript.py,但无法显示在子流程中执行的功能。尽管下面的脚本不是我正在使用的脚本,但是在尝试跟踪其功能时也会发生这种情况。

import multiprocessing

def print_hello():
    print('hello world')

def print_hello_inside_trace():
    print('trace: hello world')

def trace_function():
    print_hello_inside_trace()

if __name__ == '__main__':
    print_hello()
    process = multiprocessing.Process(target=trace_function)
    process.start()
    process.join()

所以,我的问题是:有没有办法追踪在Python子进程中执行的代码?另外,出于好奇,是否可以跟踪Python线程?

python multithreading trace multiprocess
1个回答
0
投票

我会增加日志记录。 gdb是您的朋友。您必须安装python调试扩展程序,例如py-bt为您提供python代码堆栈跟踪。常规bt将为您提供c堆栈跟踪。非常好的详细说明在这里DebuggingWithGdb

在gdb下运行它,或者如果它已经在运行,则使用pid将gdb附加到它,并使用信息线程检查线程状态。

(gdb) info threads
  Id   Target Id         Frame
  37   Thread 0xa29feb40 (LWP 17914) "NotificationThr" 0xb7fdd424 in __kernel_vsyscall ()
  36   Thread 0xa03fcb40 (LWP 17913) "python2.7" 0xb7fdd424 in __kernel_vsyscall ()
  35   Thread 0xa0bfdb40 (LWP 17911) "QProcessManager" 0xb7fdd424 

它将告诉您每个线程确切处于当前状态的位置。一旦发布更多详细信息,我们将为您提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.