Drop into an Interpreter anytime in Python

怎甘沉沦 提交于 2020-01-31 12:39:01

问题


I know how to drop into an interpreter with pdb and IPython, but this requires me knowing beforehand exactly where I want to stop. However, I often run number crunching scripts that take minutes to hours, and I would like to know exactly what it's progress is. One solution is to simply put lots of logging statements everywhere, but then I either inundate myself with too much information or fail to log exactly what I want to know.

Is there a way to initialize a listener loop that under some key combination will drop me into the code wherever it currently is? Think CTRL+Z but leaving me in Python rather than Bash.


回答1:


You can use the signal module to setup a handler that will launch the debugger when you hit control-C or control-Z or whatever.. SIGINTR, SIGSUSP.

For example, define a module instant_debug.py that overrides SIGQUIT,

import signal
import pdb

def handler(signum, frame):
  pdb.set_trace()

signal.signal(signal.SIGQUIT, handler)

Then make a script

import instant_debug
import time

for i in xrange(1000000):
  print i
  time.sleep(0.1)

At any point during execution, you can jump into the code by typing CTRL+\, examine the stack with u and d as in normal pdb, then continue with c as if nothing ever happened. Note that you will only jump in at the end of the next "atomic" operation -- that means no stopping in the middle of a giant C module.




回答2:


You could do this

def main():
    i = 1000
    while True:
        print "Count Down %s" % i
        time.sleep(1)
        i -= 1

try:
    main()
except KeyboardInterrupt:
    pass # Swallow ctrl-c
finally:
    code.interact("Dropped into interpreter", local=globals())


来源:https://stackoverflow.com/questions/10255608/drop-into-an-interpreter-anytime-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!