1

I'm currently trying to debug memory usage of a python flask program, and I'm able to run the program in PDB by adding:

import pdb
pdb.set_trace()

At the entry point of my server. Then I tell PDB to continue and run the program.

I've read that by doing Ctrl-C, I can pause a program running in PDB, but it appears Flask is catching the KeyboardInterrupt exception:

^CKeyboardInterrupt
Traceback (most recent call last):
  File "./server.py", line 4, in <module>
    from mauie.defaults import *
  File "/lib/python2.7/site-packages/flask_socketio/__init__.py", line 414, in run
  File "/lib/python2.7/site-packages/gevent/baseserver.py", line 284, in serve_forever
  File "/lib/python2.7/site-packages/gevent/event.py", line 77, in wait
  File "/lib/python2.7/site-packages/gevent/hub.py", line 338, in switch
KeyboardInterrupt

But the PDB prompt never comes up.

Charlie
  • 1,646
  • 5
  • 22
  • 40
  • Not sure why you would think that pressing ctrl-c would trigger PDB. You need to create a breakpoint where you want the code to stop. – Daniel Roseman Jan 11 '17 at 19:09
  • http://stackoverflow.com/questions/10239760/interrupt-pause-running-python-program-in-pdb - This question mentions it possible, and it works outside of Flask. It's that flask intercepts the exception so PDB never sees it. – Charlie Jan 11 '17 at 19:24

1 Answers1

0

set_trace() is simply a Python function, so you can call it at any point in your code. Have you considered simply adding additional breakpoints to your code and looking at the memory that way?

nikijean
  • 1
  • 1
  • That's the thing, I'm trying to hunt down a memory problem, but we don't know where in the code it happens. Ideally I'd like to be able to pause the process when the issue has happened, and then investigate from there. – Charlie Jan 11 '17 at 19:22
  • If it's really just a memory issue you're trying to solve, maybe you'd be better off using a profiler. There are several profilers available for python, including a pure memory profiler: [pypi/memory_profiler](https://pypi.python.org/pypi/memory_profiler) – nikijean Jan 11 '17 at 19:42