2

When a python interpreter gets killed with a signal, it typically produces output like:

 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.

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. It appears that python interpreters do not have that behavior.

Community
  • 1
  • 1
seewalker
  • 1,123
  • 10
  • 18
  • 2
    The first google hit for "python backtrace" is https://docs.python.org/2/library/traceback.html. Is that what you're looking for? I guess you want to be able to trigger this with a signal, like `kill -USR1 python_pid`, though, so you'd need to have this run from a signal handler. And have it show a backtrace for the main execution context, not the signal handler context. – Peter Cordes Nov 07 '15 at 01:16
  • Yes, code described in the page you linked to would definitely allow one to add to their own programs a mechanism for listening for input and printing a stack trace at appropriate times. I'm wondering whether any python interpreters, like cython or pypy, have something like that already built into them that I can use on any program without writing any extra code. – seewalker Nov 07 '15 at 01:21
  • 2
    I didn't see any mention of built-in ways to trigger backtraces without exitting, in this question. http://stackoverflow.com/questions/3702675/how-to-print-the-full-traceback-without-halting-the-program. But that question was asking about catching exceptions, not just seeing what your python program was up to at the moment. (i.e. taking a sample like a sample-based profiler would.) – Peter Cordes Nov 07 '15 at 01:27
  • Possible duplicate of [How to print the full traceback without halting the program?](https://stackoverflow.com/questions/3702675/how-to-print-the-full-traceback-without-halting-the-program) – pppery Jul 31 '19 at 03:37

1 Answers1

0
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

repzero
  • 8,254
  • 2
  • 18
  • 40