简体   繁体   English

如何在不退出程序的情况下在linux上打印最近的最后一次调用

[英]How to print the most recent last call on linux without exiting the program

When I execute a python script on linux and interrupt the process, it gives me the " most recent call last": 当我在linux上执行python脚本并中断该过程时,它给了我“最近一次通话”:

Traceback (most recent call last):
  File "clustermptest.py", line 38, in <module>
    if sequences[i][x:x+3]==CAI[cailength][1]:
KeyboardInterrupt

Is there a way to see what the most recent call last is ie the line it is currently processing while the script is still running/without stopping the script? 有没有办法查看最近一次调用的最新内容,即脚本仍在运行/不停止脚本时当前正在处理的行?

You could use winpdb to attach to your running program and allow you to break, inspect, and resume it at various points. 您可以使用winpdb 附加到正在运行的程序,并允许您在各个点上对其进行中断,检查和恢复。


Or, you could define a signal handler, although this may not be a completely robust solution especially if you are using threads: 或者,您可以定义一个信号处理程序,尽管这可能不是一个完全可靠的解决方案,尤其是在使用线程的情况下:

import signal
import traceback

def sigint_handler(signal, frame):
    # ctrl-c generates SIGINT
    traceback.print_stack()
    print('-'*80)

def foo():
    total = 0
    for i in range(10**8):
        total += i
    return total

if __name__=='__main__':
    signal.signal(signal.SIGINT, sigint_handler)
    print(foo())

Running test.py , and pressing Cc at various moments yields: 运行test.py ,并在不同时间点按Cc产生:

  C-c C-c  File "/home/unutbu/pybin/test.py", line 20, in <module>
    print(foo())
  File "/home/unutbu/pybin/test.py", line 14, in foo
    for i in range(10**8):
  File "/home/unutbu/pybin/test.py", line 9, in sigint_handler
    traceback.print_stack()
--------------------------------------------------------------------------------
  C-c C-c  File "/home/unutbu/pybin/test.py", line 20, in <module>
    print(foo())
  File "/home/unutbu/pybin/test.py", line 15, in foo
    total += i
  File "/home/unutbu/pybin/test.py", line 9, in sigint_handler
    traceback.print_stack()
--------------------------------------------------------------------------------
4999999950000000

References: 参考文献:

  1. The signal module (be sure to read the caveats) 信号模块 (请务必阅读警告)
  2. The traceback module 追溯模块
  3. Doug Hellman's Python Module of the Week tutorial on using signal 道格·海尔曼(Doug Hellman)的每周Python模块使用信号教程

暂无
暂无

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

相关问题 如何修复 Selenium Traceback(最近一次调用最后一次):print(contact.text()) AttributeError: 'list' object has no attribute 'text' - How To Fix Selenium Traceback (most recent call last): print(contact.text()) AttributeError: 'list' object has no attribute 'text' 我如何摆脱这个错误:Traceback(最近一次调用最后一次):文件“<string> ",第 1 行,在<module>文件“C:\程序?</module></string> - How Do I get rid of this error:Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Program? 如何在不停止/退出程序的情况下捕获并打印完整的异常回溯? - How to catch and print the full exception traceback without halting/exiting the program? Python Selenium Traceback(最近一次调用最后一次): - Python Selenium Traceback (most recent call last): 解释“回溯(最近一次调用最后一次):”错误 - Interpreting a “Traceback (most recent call last):” error 蟒蛇-追溯(最近一次呼叫过去):… - python - Traceback (most recent call last): … Python TypeError Traceback(最近一次调用最后一次) - Python TypeError Traceback (most recent call last) 回溯(最近一次调用):Python - Traceback (most recent call last): Python Traceback(最近一次通话最后一次)Python 错误 - Traceback (most recent call last) Python Error Python追踪(最近一次通话)错误 - Python Traceback (most recent call last) error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM