如何在uWSGI下调试python应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18427948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:44:08  来源:igfitidea点击:

How to debug python application under uWSGI?

pythonuwsgi

提问by Anderson

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

当我尝试在 uWSGI 下使用 python pdb 调试器时,执行不会在断点处停止,它只是返回引用。

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)

采纳答案by roberto

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

作为服务器,uWSGI 关闭标准输入(有效地将其重新映射到 /dev/null)。

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

如果您需要 stdin(就像您需要终端调试器一样),请添加:

--honour-stdin

回答by Mohamed Challouf

try this

尝试这个

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

回答by cdosborn

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

通过 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.

您可能希望使用单个工作线程/线程运行 uWSGI,以便远程调试器不会相互干扰。