如何配置uWSGI以便使用pdb进行调试(--遵守stdin配置问题)



我希望能够在uWSGI下使用pdb调试Python(Django(应用程序,我基本上遇到了与这里描述的相同的问题:

...
File "/usr/lib/python2.7/bdb.py", line 49, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib/python2.7/bdb.py", line 68, in dispatch_line
if self.quitting: raise BdbQuit
BdbQuit

不同的是,我有一个不同的uWSGI设置,并且我似乎无法按照上面问题的公认答案中的建议进行uWSGIhonour-stdin

我的设置如下:

1( 我有一个在皇帝模式中启动uWSGI的系统过程

[Unit]
Description=uWSGI Emperor service
[Service]
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/emperor.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target

2(/etc/uwsgi/emperor.ini看起来像这样:

[uwsgi]
emperor = /etc/uwsgi/sites
uid = www-data
gid = www-data
limit-as = 1024
logto = /tmp/uwsgi-emperor.log
# I've tried adding both honour-stdin 
# and daemons-honour-stdin here
honour-stdin = true
daemons-honour-stdin = true

3( 其中一个uwsgi站点的示例配置如下:

#/etc/uwsgi/sites/testproject.ini
[uwsgi]
module = wsgi
chdir = /home/myuser/projects/testproject
home = /home/myuser/.virtualenvs/testproject
env = DJANGO_SETTINGS_MODULE=testproject.settings.dev
daemonize = /tmp/uwsgi-testproject.log
master = true
processes = 1
socket = /tmp/testproject-dev.sock
chmod-socket = 664
vacuum = true
# I've also tried adding both honour-stdin 
# and daemons-honour-stdin here
honour-stdin = true
daemons-honour-stdin = true

4( 我不确定它是否与这个问题有关,但我也有一个nginx配置来服务这个网站,它看起来像这样:

upstream app-testproject-dev {
server unix:///tmp/testproject-dev.sock;
}
server {
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
listen 80;
server_name dev.testproject.com;
location / {
uwsgi_pass app-testproject-dev;
include /etc/nginx/uwsgi_params;
}
}

目前的一个临时解决方案是使用remote-pdb作为我的方法的替代方案,但我有兴趣了解我当前配置设置中的问题以及如何解决它

UPDATE:我刚刚意识到,即使这样做有效,可能我没有正确打开日志文件,因此pdb可以等待我的输入。现在我正在使用tail来查看日志的情况,但不知道这是否适用于pdb

UPDATE2:做了更多的测试,通过以下方式启动守护进程,尝试跳过等式中的systemd+uwsgi emperor模式:

sudo /usr/local/bin/uwsgi --ini /etc/uwsgi/sites/testproject.ini

我注意到,在.ini文件中没有daemonize = /tmp/uwsgi-testproject.log,一切都很好,但一旦我对其进行后台处理,stdin就会开始指向/dev/null(我将honor stdin和后台进程honor stdin都设置为true(。我正在和核实

ls -l /proc/<proc_id>/fd/0

我不知道这能帮你解决问题。但是,请尝试使用以下代码配置您的uWSGI。

将放入/etc/systemd/system/emperor.uwsgi.service

[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/usr/bin/uwsgi --ini /etc/uwsgi/emperor.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target

放入/etc/uwsgi/emperor.ini

[uwsgi]
master = true
procname-master = Emperor
; Look for vassal configs using this pattern
emperor = /srv/apps/*/uwsgi.ini
; Don't resolve symlinks when considering reload-on-touch
emperor-nofollow = true
; Lowest privs
uid = www-data
gid = www-data
; Clean up our workers when we die.
no-orphans = true

每个站点的配置(在uWSGI术语中称为"附庸"(。

放入/etc/uwsgi/vassal.ini

[uwsgi]
master = true
procname-master = %c
; Run with lower privs
uid = www-data
gid = www-data
; :0 lets the OS assign a port
socket = 127.0.0.1:0
; Register with the FastRouter the list of hostnames
subscribe-to = 127.0.0.1:9001:@hostnames.txt
; Paths are referenced relative to where the INI file is found
chdir = %d
# Task management
; Max 4 processes
processes = 4
; Each running 4 threads
threads = 4
; Reduce to 1 process when quiet
cheaper = 1
; Save some memory per thread
thread-stack-size = 512
# Logging
plugin = logfile
req-logger = file:logs/request.log
logger = file:logs/error.log
log-x-forwarded-for = true
# Python app
plugin = python
virtualenv = venv/
pythonpath = code/
module = %c.wsgi
enable-threads = true
# Don't load the app in the Master.
app-lazy = true

最后是Fastrouter。这就像一个智能负载均衡器,但会根据主机名来分配请求。

将放入/etc/uwsgi/router.ini

[uwsgi]
master = true
procname-master = FastRouter
uid = www-data
gid = www-data
plugin = fastrouter
fastrouter = %d/server.sock
; run as lower privs
fastrouter-uid = www-data
fastrouter-gid = www-data
; handle the scale
fastrouter-processes = 2
; but scale down when quiet
fastrouter-cheap = true
; let others vassals subscribe to us
fastrouter-subscription-server = 127.0.0.1:9001
# Logging
plugin = logfile
req-logger = file:logs/request.log
logger = file:logs/error.log

有了订阅服务器,附庸可以向FastRouter注册,告诉它他们可以处理哪些主机名,以及可以在哪个端口上联系他们。

然后使用启动

# systemctl start emperor.uwsgi

如果这项工作没有问题,请启用它在启动时启动:

# systemctl enable emperor.uwsgi

最新更新