简体   繁体   English

检查正在运行的Python解释器

[英]Examining Running Python Interpreter

When a python interpreter gets killed with a signal, it typically produces output like: 当python解释器被信号杀死时,通常会产生如下输出:

 File "~/anaconda/envs/py34/lib/python3.4/site-packages/skimage/feature/orb.py", line 313, in detect_and_extract
orientations)
  File "~/anaconda/envs/py34/lib/python3.4/site-packages/skimage/feature/orb.py", line 218, in _extract_octave
descriptors = _orb_loop(octave_image, keypoints, orientations)
  File "skimage/feature/orb_cy.pyx", line 56, in skimage.feature.orb_cy._orb_loop (skimage/feature/orb_cy.c:2276)
  File "~/anaconda/envs/py34/lib/python3.4/site-packages/numpy/core/numeric.py", line 394, in asarray
def asarray(a, dtype=None, order=None):
KeyboardInterrupt

Is there a way to produce output like that as part of pausing, rather than permanently stopping, the running process? 有没有一种方法可以产生这样的输出,作为暂停而不是永久停止正在运行的过程的一部分? It would be cool to be able to inspect running python programs like that, without slowing them down by management of a debugger. 能够检查正在运行的python程序而不用调试器的管理来降低它们的速度,这将是很酷的。

To people reading this question to see whether it relates to their own technical issue: 对于阅读此问题的人们,看它是否与他们自己的技术问题有关:

This popular page explains how to programmatically get traceback output without halting. 这个受欢迎的页面介绍了如何以编程方式获取回溯输出而不会暂停。 My question asked about whether python interpreters have such code built into them (so that one could give a signal like kill -n for some n in order to get traceback output without stopping the process), so that one does not have to explicitly add that to their program. 我的问题询问是否在python解释器中内置了这样的代码(以便可以在某个n发出类似kill -n的信号,以便在不停止进程的情况下获得回溯输出),这样就不必显式添加该代码了。他们的程序。 It appears that python interpreters do not have that behavior. 看来python解释器没有这种行为。

from time import sleep
import sys,traceback
try:
    print("hello")
    sleep(3)
except KeyboardInterrupt as error:
    print("This is my traceback")
    traceback.print_exc(sys.stdout)

You can place you pause function after printing your traceback 您可以在打印回溯后放置暂停功能

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

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