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.
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.pythat overrides SIGQUIT,Then make a script
At any point during execution, you can jump into the code by typing
CTRL+\, examine the stack withuanddas in normalpdb, then continue withcas 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.