问题描述
我有一个用python编写的事件驱动应用程序。一段时间后(通常> 1周),它似乎只是停止响应事件。当发生这种情况时,我只需按ctrl-C并重新运行,一切都很好。但是,这种情况一直很烦人,我不知道是什么原因造成的。 有没有一种方法可以运行我的应用程序,当这种情况发生并且应用程序不再接受连接时,我可以进入调试器,查看其运行情况以及为什么不建立连接?
I have an event-driven application, written in python. After a while (usually >1 week) it appears to just stop responding to events. When this happens, I just ctrl-C and re-run and all is well-again. However, it's kind of annoying that this keeps happening and I have no idea what's causing it. Is there a way I can run my application that when this occurs and the application is no longer accepting connections, I can drop into a debugger and see what it's doing and why it's not taking connections?
我以前使用过pdb,但是我使用过它的方式(
,如果条件:pdb.set_trace())并不是真正适用于此,因为我不知道它失败时在代码中的作用。我的理想情况是使用Ctrl-somethingelse代替Ctrl-C,这会使它停止并进入调试器。这样的事情容易做到吗?
I've used pdb before, but the way I've used it (if condition: pdb.set_trace()
) doesn't really apply here, because I have no idea what it's doing in the code when it fails. My ideal situation would be instead of Ctrl-C maybe I hit Ctrl-somethingelse and that causes it to stop and drop into the debugger. Is such a thing easily done?
推荐答案
在您的情况下触发pdb可能并不简单。但是,每当需要调试此类挂起时,我都会使用函数。
Triggering pdb in your case is probably not simple. However, whenever I need to debug such hangs, I inspect a "snapshot" of tracebacks of all the threads in the process, using the dumpstacks()
function.
您可以使用计时器定期调用它,并将输出打印到一个日志文件,并在发现挂起时引用它,或者利用某些RPC机制(例如信号)来按需触发进程中的函数调用。我通常这样做,因为我系统中的进程已经在侦听此类RPC请求(使用rpyc)。
You can either use a timer to call it periodically and print the output to a log file, and refer to it when you notice the hanging, or harness some RPC mechanism (e.g. signals) to trigger the function call in your process on demand. I usually do the latter, because the processes in my system already listen to such RPC requests (using rpyc).
这篇关于调试只是“挂起”的python应用程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!