[英]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: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.