Casebash
Casebash

Reputation: 118782

Method to peek at a Python program running right now

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

Upvotes: 17

Views: 5503

Answers (7)

Karim H
Karim H

Reputation: 61

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.

Upvotes: 1

Denis Otkidach
Denis Otkidach

Reputation: 33200

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.

Upvotes: 0

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94475

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

Upvotes: 0

Andrew Dalke
Andrew Dalke

Reputation: 15305

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:

Upvotes: 10

Walter
Walter

Reputation: 7981

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.

Upvotes: 2

ʞɔıu
ʞɔıu

Reputation: 48386

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.

Upvotes: 26

shylent
shylent

Reputation: 10086

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

Upvotes: 4

Related Questions