2013-08-25 30 views
26

當我試圖在uWSGI下使用python pdb調試器時,執行不會停在斷點上,它只是返回引用。如何在uWSGI下調試python應用程序?

這裏是代碼:

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

我這是怎麼運行它:

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

,這就是我得到:

/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) 
+0

你是否嘗試過以不同的方式進行調試(即不是使用'set_trace()'手動插入斷點)? –

+0

我在簡單的文本編輯器中開發它,沒有任何IDE。所以我知道的唯一選擇是斷點。 – Anderson

+0

檢查這個帖子:http://stackoverflow.com/questions/6980749/simpler-way-to-put-pdb-breakpoints-in-python-code 除了一個好的IDE可以真正讓生活更容易爲你(並有助於提高生產力等)。 –

回答

47

作爲一個服務器,uWSGI關閉stdin(有效地將其重新映射到/ dev/null)。

如果您需要標準輸入(當你需要一個終端調試器)加:

--honour-stdin 
+3

有點不言而喻,但萬一別人沒有意識到這一點,你還需要確保你沒有在守護進程模式下運行uwsgi。否則,在事實之後(這甚至有可能?),你必須將終端掛接到進程的stdin/stdout。 –

+0

感謝您的提示! –

-3

試試這個

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

這不是調試,甚至沒有提供適當的python stacktrace – jrwren

+0

它不回答這個問題,但這個評論對我很有幫助。 –

9

安裝遠程調試器。

pip install remote-pdb 

在應用程序的某處設置斷點。

from remote_pdb import RemotePdb 
RemotePdb('127.0.0.1', 4444).set_trace() 
通過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 

你可能會希望有一個工作/線程運行uWSGI,使遠程調試器不會在一個又邁進了一步

連接到遠程調試器。