9

If I develop a Django app and use the included testing server, I can just add a import pdb; pdb.set_trace() anywhere in the code and have a breaking point that throws me into an interactive debugger. To make things clear, I dont' mean using any IDE, just simple setup of ssh-ing into a VM or remote dev server.

How can I get a similar behavior for an WSGI Django app? (again, the assumed setup is me with an ssh session to the server - VM or remote)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
NeuronQ
  • 7,527
  • 9
  • 42
  • 60

2 Answers2

9

To the best my knowledge, if you want to use pdb with arbitrary a wsgi app, your best bet is to use rpdb2.

(for Django on mod_wsgi with apache, you can refer to this guide: http://code.google.com/p/modwsgi/wiki/DebuggingTechniques)

K Z
  • 29,661
  • 8
  • 73
  • 78
  • Based on link to rpdb2 I found nice way to debug things in django running eg. gunicorn http://stackoverflow.com/a/23106910/479931 . Thanks @KayZhu – lechup Apr 16 '14 at 10:44
0

The problem is that WSGI servers will use multiple processes and detach or redirect the standard streams - in, out, and shake it all about err.

For Apache httpd and mod_wsgi:

  • Start with apachectl -X, not apachectl start (nor service apache2 start, etc.)
  • Do not use WSGIDaemonProcess, WSGIProcessGroup, etc.
    You may need to add WSGIPythonHome, WSGIPythonPath, etc. to replace the daemon properties.

Similarly, for gunicorn you need the settings debug=True and daemon=False.


The mod_wsgi guide recommends wrapping wsgi.application with a class, invoking pdb at the start of every request, but any other traps should work.

class Debugger:
    def __init__(self, object):
        self.__object = object

    def __call__(self, *args, **kwargs):
        import pdb, sys
        debugger = pdb.Pdb()
        debugger.use_rawinput = 0
        debugger.reset()
        sys.settrace(debugger.trace_dispatch)
        try:
            return self.__object(*args, **kwargs)
        finally:
            debugger.quitting = 1
           sys.settrace(None)

application = Debugger(application)
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • For mod_wsgi better off using ``mod_wsgi-express`` and then use the ``--enable-debugger`` to ``start-server``. It does everything to allow you to use the debugger. – Graham Dumpleton May 17 '18 at 04:31