Question

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)

Était-ce utile?

La solution

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)

Autres conseils

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top