Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Gunicorn Debug

Initially I had a Django app with the included testing server. To debug this setup, 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 in Terminal (on command-line).

Recently I shifted to gunicorn to gain some perf benifits. How can I get a similar behavior while using this Gunicorn setup. I have tried by setting gunicorn settings with debug=True and daemon=False but it does not work.

Anyone has a solution to this?

like image 560
Srikar Appalaraju Avatar asked Nov 03 '12 15:11

Srikar Appalaraju


3 Answers

To run green unicorn in a reverse proxy configuration (under nginx) in a debugger / debug mode, enter the following settings in PyCharm's Run / Django / Edit configurations:

Of course, use whatever port (instead of 7777) you have configured your nginx to proxy to.

Screenshot

like image 132
Kimvais Avatar answered Sep 23 '22 20:09

Kimvais


Ok I've recently stumbed upon similar problem. I wasn't able to apply @dudklein solution (I get I/O Errors while debugger was trying to take input - ipdb, pdb etc.)

I used remote python debbuger - winpdb and it's embedded debugging.

  1. install winpdb in Your virtualenv

    pip install winpdb
    
  2. import and run embedded debugger in Your code:

    import rpdb2
    rpdb2.start_embedded_debugger('pass')
    
  3. run gunicorn with --timeout argument

    gunicorn -t 3600 env:application
    
  4. run proper view using browser eg. http://127.0.0.1:8000/your-view/

  5. connect to embedded debugger using winpdb:

    winpdb -a /path/to/django/app/views.py
    

    It will prompt You for a password (use one You set in Your code, in my example it is 'pass') and run nice GUI with debugger.

  6. if You need tutorial for winpdb - here You are.

enter image description here

like image 45
lechup Avatar answered Sep 24 '22 20:09

lechup


If you are able to launch gunicorn pointing at an application instance that is an instance of the DebuggedApplication class from the werkzeug library, you will be able to set break points using the werkzeug debugger with import ipdb; ipdb.set_trace() right in your browser.

import django.core.handlers.wsgi
from werkzeug.debug import DebuggedApplication

application = django.core.handlers.wsgi.WSGIHandler()
application = DebuggedApplication(application, evalex=True)

Make sure you install werkzeug library and ipdb of course. (pip install werkzeug and pip install ipdb)

like image 42
Calvin Cheng Avatar answered Sep 20 '22 20:09

Calvin Cheng