54

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)
Anderson
  • 573
  • 1
  • 4
  • 9
  • Have you tried debugging it differently (i.e. rather than using `set_trace()` with manually inserting breakpoints)? – Aleksander Lidtke Aug 25 '13 at 10:35
  • I'm developing it in simple text editor, without any IDE. So the only option I'm aware of is breakpoints. – Anderson Aug 25 '13 at 11:04
  • Check this post out: http://stackoverflow.com/questions/6980749/simpler-way-to-put-pdb-breakpoints-in-python-code Besides a good IDE can really make life a lot easier for you (and help to increase the productivity etc.). – Aleksander Lidtke Aug 25 '13 at 11:10

3 Answers3

84

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
David Wolever
  • 148,955
  • 89
  • 346
  • 502
roberto
  • 12,723
  • 44
  • 30
  • 8
    Kinda goes without saying, but just in case other people don't realize this you also need to make sure you're not running uwsgi in daemon mode. Otherwise, you'll have to hook a terminal up to the process's stdin/stdout after the fact (is this even possible?). – David Sanders Dec 02 '13 at 23:11
  • 1
    @DavidSanders How do you run uwsgi without daemon mode? – alexandernst Apr 20 '18 at 22:49
  • `uwsgi --http-socket :8000 --wsgi-file uwsgi_test.py --callable application --virtualenv /opt/virtualenvs/uwsgi_test --honour-stdin` worked for me – edilio Jul 26 '18 at 08:52
  • 1
    Can add `--workers 1` so you only have one worker to worry about, and `--harakiri 1200` to give you 20mins of debugging, else its likely to respawn after say 60s. – run_the_race Oct 23 '20 at 15:52
24

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.

cdosborn
  • 3,111
  • 29
  • 30
-6

try this

uwsgi --http 127.0.0.1:7777  --wsgi-file uwsgi_test.py --logto /path/to/log/log.txt
Sliq
  • 15,937
  • 27
  • 110
  • 143
Mohamed Challouf
  • 105
  • 1
  • 1
  • 8