Is it possible to find any information about what a Python program running right now is doing without interrupting it?
Also, if it isn't possible, is there anyway to crash a running Python program so that I can at least get a stacktrace (using PyDev on Ubuntu)?
I know I should开发者_运维技巧 have used logs or run it in debug mode or inserted a statement to run the debugger...
Related questions
- Getting stack trace from a running Python program - Very similar, but more general, this question was intended to be about debugging a Python program that is running right now.
If you place
import code
code.interact(local=locals())
at any point in your script, python will instantiate a python shell at exactly that point that has access to everything in the state of the script at that point. ^D exits the shell and resumes execution past that point.
You can even modify the state at that point from the shell, call functions, etc.
If you have a running Python, which wasn't built with any sort of trace or logging mechanism, and you want to see what it's doing internally, then two options are:
On a Solaris or Mac, if you are using the system-provided Python then use dtrace
use gdb to attach to a running Python process,
To "crash" a python program with a stacktrace you can send it SIGINT, that is unless you trap it or catch KeyboardInterrupt (python installs a SIGINT handler by default, that raises KeyboardInterrupt).
As for debugging, doesn't PyDev have built-in debugging support (through pdb)?
Personally, I prefer ipdb. It's pdb with added IPython goodness. It seems to be more of an interactive Python interpreter with a few shortcuts for debugging functions.
If you're happy with a crash, inserting "1/0" will create a quick and dirty breakpoint, with a complete backtrace!
Install signal handler that sets a trace function with sys.settrace()
that prints traceback and clears clears trace function. This will allow you to see where your program is at any moment without interrupting it. Note, that signal is handled after each sys.getcheckinterval()
python instructions.
You could use lptrace for that. It's like strace
for Python programs – it lets you attach to a running Python process and prints every function call it makes.
精彩评论