简体   繁体   English

调试:如何检查我的Python程序挂在哪里?

[英]debugging: how to check what where my Python program is hanging?

A fairly large Python program I write, runs, but sometimes, after running for minutes or hours, in a non easily reproducible moment, hangs and outputs nothing to the screen. 我编写的一个相当大的Python程序可以运行,但是有时,在运行数分钟或数小时后,在不容易再现的瞬间,它挂起并且没有任何输出到屏幕。

I have no idea what it is doing at that moment, and in what part of code it is. 我当时不知道它在做什么,它在代码的哪一部分。

How can I run this in a debugger or something to see what lines of code is the program executing in the moment it hangs? 如何在调试器或其他工具中运行此程序,以查看程序在挂起时正在执行哪些代码行?

Its too large to put "print" statements all over the place. 它太大,无法在各处放置“打印”语句。

I did: 我做了:

python -m trace --trace /usr/local/bin/my_program.py

but that gives me so much output that I can't really see anything, just millions of lines scrolling on the screen. 但这给了我很多输出,我什至看不到任何东西,只有几百万行在屏幕上滚动。

Best would be if I could send some signal to the program with "kill -SIGUSR1" or something, and at that moment the program would drop into a debugger and show me the line it stopped at and possibly allow me to step through the program then. 最好的情况是,我可以使用“ kill -SIGUSR1”或类似的命令向程序发送一些信号,然后程序会进入调试器并向我显示它停止的那一行,并可能允许我逐步执行该程序,然后。

I've tried: 我试过了:

pdb usr/local/bin/my_program.py

and then: 然后:

(Pdb) cont

but what do I do to see where I am when it hangs? 但是当挂起时我该怎么做呢? It doesn't throw and exception, just seems like it waits for something, possibly in an infinite loop. 它不会引发异常,就像它在等待某些东西一样,可能处于无限循环中。

One more detail: when the program hangs, and I press ^C and then (not sure if that is necessary) the program continues normally (without throwing any exception and without giving me any hint on the screen why did it stop). 更详细一点:当程序挂起时,我按^ C键,然后(不确定是否有必要)该程序正常运行(没有引发任何异常,并且没有在屏幕上提示我为什么停止)。

This could be useful to you. 这对您可能有用。 I usually do 我通常会

>>> import pdb
>>> import program2debug
>>> pdb.run('program2debug.test()')

I usually add a -v option to my programs, which enables tons of print statements explaining what I'm doing in detail. 通常,我会在程序中添加-v选项,从而启用大量的打印语句来详细说明我的操作。 When you write a program in the future, consider doing the same before it gets thousands of lines big. 将来编写程序时,请考虑先做同样的事情,然后再增加数千行。

You could try running it in debug mode in an IDE like pydev (eclipse) or pycharm. 您可以尝试在像devdev(eclipse)或pycharm这样的IDE中以调试模式运行它。 You can break the program at any moment and get to its current execution point. 您可以随时中断程序并转到其当前执行点。

No program is ever too big to put print statements all over the place. 没有哪个程序太大,无法在各处放置打印语句。 You need to read up on the logging module and insert lots of logging.debug() statements. 您需要阅读logging模块并插入大量logging.debug()语句。 This is just a better form of print statement that outputs to a file, and can be turned off easily in production software. 这只是输出到文件的打印语句的一种更好形式,可以在生产软件中轻松关闭它。 But years from now, when you need to modify the code, you can easily turn it all back on and get the benefit of the insight of the original programmer. 但是从现在开始,多年后,当您需要修改代码时,您可以轻松地将其重新打开,并从原始程序员的见解中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM