16

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

Community
  • 1
  • 1
Casebash
  • 114,675
  • 90
  • 247
  • 350

7 Answers7

26

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.

ʞɔıu
  • 47,148
  • 35
  • 106
  • 149
  • Nice. I didn't know that (+1) – shylent Oct 28 '09 at 13:36
  • 2
    Interesting. How does this differ from import pdb; pdb.set_trace( )? – Jon Hadley Oct 28 '09 at 13:46
  • 1
    pdb gives you stepInto stepOut etc, etc and is its own special shell that you have to learn how to use that has step control over execution of the entire program. code.interact() is exactly the normal python shell that we all know and love. pdb is much more powerful and often overkill and difficult to use unless you know it well. – ʞɔıu Oct 28 '09 at 13:56
  • 1
    Sorry for being unclear. I can't modify the script as it is already running – Casebash Oct 28 '09 at 21:16
  • Your options for inspecting script execution (that I know of) are this thing, pdb or trace, but all of these require being able to modify the script. I don't think it's feasible while the script is running without modifying it. – ʞɔıu Oct 28 '09 at 22:09
  • 1
    +1 for giving an interesting way to debug a program, even if not related to the question. – Joël Nov 10 '11 at 08:14
10

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:

Andrew Dalke
  • 14,889
  • 4
  • 39
  • 54
4

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)?

shylent
  • 10,076
  • 6
  • 38
  • 55
  • which means you get the process id and do 'kill -INT $pid' . That will kill it, but should give you a stack trace. It does assume you have some way to see the stderr output. – Andrew Dalke Oct 28 '09 at 22:28
2

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.

Walter
  • 7,809
  • 1
  • 30
  • 30
1

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.

Karim H
  • 61
  • 2
0

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.

Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100
0

If you're happy with a crash, inserting "1/0" will create a quick and dirty breakpoint, with a complete backtrace!

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260