How to debug python application under uWSGI?

PythonUwsgi

Python Problem Overview


When I'm trying to use python pdb debugger under uWSGI, the execution doesn't stop on breakpoint, it just return trackback.

here is the code:

def application(env, start_response):
    import pdb; pdb.set_trace()
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

this is how I run it:

uwsgi --http 127.0.0.1:7777  --wsgi-file uwsgi_test.py

and this is what I get:

/home/andrey/Development/ttt/uwsgi_test.py(3)application()
-> start_response('200 OK', [('Content-Type','text/html')])
(Pdb) 
Traceback (most recent call last):
  File "uwsgi_test.py", line 3, in application
    start_response('200 OK', [('Content-Type','text/html')])
  File "uwsgi_test.py", line 3, in application
    start_response('200 OK', [('Content-Type','text/html')])
  File "/usr/lib/python2.7/bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python2.7/bdb.py", line 67, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit
[pid: 11421|app: 0|req: 1/1] 127.0.0.1 () {32 vars in 366 bytes} [Sun Aug 25 13:12:06 2013] GET / => generated 0 bytes in 63 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

Python Solutions


Solution 1 - Python

Being a server, uWSGI closes the stdin (effectively it remaps it to /dev/null).

If you need stdin (as when you need a terminal debugger) add:

--honour-stdin

Solution 2 - Python

Install remote debugger.

pip install remote-pdb

Set breakpoint somewhere in application.

from remote_pdb import RemotePdb
RemotePdb('127.0.0.1', 4444).set_trace()

Connect to remote debugger via telnet

# Restart uwsgi to pick up changes
...

# Trigger the breakpoint, (any action to evaluate the set_trace call)
...
 
# Connect to debugger
telnet 127.0.0.1 4444

You will likely want to run uWSGI with a single worker/thread, so that the remote debuggers do not step on one another.

Solution 3 - Python

try this

uwsgi --http 127.0.0.1:7777  --wsgi-file uwsgi_test.py --logto /path/to/log/log.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndersonView Question on Stackoverflow
Solution 1 - PythonrobertoView Answer on Stackoverflow
Solution 2 - PythoncdosbornView Answer on Stackoverflow
Solution 3 - PythonMohamed ChalloufView Answer on Stackoverflow