Django中的Python代码没有使用Apache / mod_wsgi以守护进程模式重新编译

时间:2017-01-05 13:00:11

标签: python django apache2 mod-wsgi

我在守护进程模式下使用带有mod_wsgi的Apache2运行Django应用程序:

WSGIApplicationGroup %{GLOBAL}

WSGIDaemonProcess my_app_process
WSGIScriptAlias /my_app /var/www/my_app/wsgi.py process-group=my_app_process
WSGIScriptReloading On

每当我在服务器上更改我的Python代码时(我知道这是非常气馁的,但我只是用它来进行调试)我一直在重新编译代码时遇到麻烦,但只是在子目录中。

应用程序根目录中的Python文件(即settings.py)很好地重新编译,但不是位于子目录中的Python文件(即某些应用程序的views.py)。

应用程序目录由www-data用户拥有,并且用户具有足够的权限(使用777进行测试)。

重新启动Apache或触摸wsgi文件只会导致重新编译应用程序根目录中的Python文件。

修改

我还尝试添加WSGIProcessGroup指令但没有效果:

WSGIApplicationGroup %{GLOBAL}

WSGIDaemonProcess my_app_process processes=2 threads=15
WSGIProcessGroup my_app_process
WSGIScriptAlias /my_app /var/www/my_app/wsgi.py
WSGIScriptReloading On

另外,使用提供的代码here我已经得出结论,事实上,我正在以守护进程模式运行。

编辑#2

从重新启动添加了Apache日志:

[Fri Jan 06 12:03:06.309060 2017] [wsgi:warn] [pid 28372:tid 139790316758912] mod_wsgi: Compiled for Python/2.7.8.
[Fri Jan 06 12:03:06.309186 2017] [wsgi:warn] [pid 28372:tid 139790316758912] mod_wsgi: Runtime using Python/2.7.9.
[Fri Jan 06 12:03:06.309876 2017] [wsgi:info] [pid 28375:tid 139790316758912] mod_wsgi (pid=28375): Starting process 'my_app' with uid=33, gid=33 and threads=15.
[Fri Jan 06 12:03:06.310193 2017] [wsgi:info] [pid 28375:tid 139790316758912] mod_wsgi (pid=28375): Initializing Python.
[Fri Jan 06 12:03:06.310246 2017] [wsgi:info] [pid 28376:tid 139790316758912] mod_wsgi (pid=28376): Starting process 'my_app_2' with uid=33, gid=33 and threads=15.
[Fri Jan 06 12:03:06.310531 2017] [wsgi:info] [pid 28377:tid 139790316758912] mod_wsgi (pid=28377): Starting process 'my_app_3' with uid=33, gid=33 and threads=15.
[Fri Jan 06 12:03:06.310607 2017] [wsgi:info] [pid 28376:tid 139790316758912] mod_wsgi (pid=28376): Initializing Python.
[Fri Jan 06 12:03:06.310855 2017] [wsgi:info] [pid 28377:tid 139790316758912] mod_wsgi (pid=28377): Initializing Python.
[Fri Jan 06 12:03:06.311029 2017] [mpm_event:notice] [pid 28372:tid 139790316758912] AH00489: Apache/2.4.10 (Debian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Fri Jan 06 12:03:06.311057 2017] [mpm_event:info] [pid 28372:tid 139790316758912] AH00490: Server built: Sep 15 2016 20:44:43
[Fri Jan 06 12:03:06.311071 2017] [core:notice] [pid 28372:tid 139790316758912] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jan 06 12:03:06.311379 2017] [wsgi:info] [pid 28379:tid 139790316758912] mod_wsgi (pid=28379): Initializing Python.
[Fri Jan 06 12:03:06.311507 2017] [wsgi:info] [pid 28378:tid 139790316758912] mod_wsgi (pid=28378): Initializing Python.
[Fri Jan 06 12:03:06.324667 2017] [wsgi:info] [pid 28377:tid 139790316758912] mod_wsgi (pid=28377): Attach interpreter ''.
[Fri Jan 06 12:03:06.324671 2017] [wsgi:info] [pid 28376:tid 139790316758912] mod_wsgi (pid=28376): Attach interpreter ''.
[Fri Jan 06 12:03:06.324843 2017] [wsgi:info] [pid 28377:tid 139790316758912] mod_wsgi (pid=28377): Adding '/var/anaconda2/lib/python2.7/site-packages' to path.
[Fri Jan 06 12:03:06.324864 2017] [wsgi:info] [pid 28376:tid 139790316758912] mod_wsgi (pid=28376): Adding '/var/anaconda2/lib/python2.7/site-packages' to path.
[Fri Jan 06 12:03:06.324988 2017] [wsgi:info] [pid 28378:tid 139790316758912] mod_wsgi (pid=28378): Attach interpreter ''.
[Fri Jan 06 12:03:06.329741 2017] [wsgi:info] [pid 28379:tid 139790316758912] mod_wsgi (pid=28379): Attach interpreter ''.
[Fri Jan 06 12:03:06.335879 2017] [wsgi:info] [pid 28375:tid 139790316758912] mod_wsgi (pid=28375): Attach interpreter ''.
[Fri Jan 06 12:03:06.336042 2017] [wsgi:info] [pid 28375:tid 139790316758912] mod_wsgi (pid=28375): Adding '/var/anaconda2/lib/python2.7/site-packages' to path.
[Fri Jan 06 12:03:13.598083 2017] [wsgi:info] [pid 28375:tid 139790204262144] [remote 10.6.6.58:512] mod_wsgi (pid=28375, process='my_app', application=''): Loading WSGI script '/var/www/my_app/wsgi.py'.

通过触摸WSGI脚本添加了Apache日志:

[Fri Jan 06 12:12:28.348075 2017] [wsgi:info] [pid 28835:tid 139790316758912] mod_wsgi (pid=28835): Shutdown requested 'my_app'.
[Fri Jan 06 12:12:28.348217 2017] [wsgi:info] [pid 28835:tid 139790316758912] mod_wsgi (pid=28835): Stopping process 'my_app'.
[Fri Jan 06 12:12:28.348237 2017] [wsgi:info] [pid 28835:tid 139790316758912] mod_wsgi (pid=28835): Destroying interpreters.
[Fri Jan 06 12:12:28.348244 2017] [wsgi:info] [pid 28835:tid 139790316758912] mod_wsgi (pid=28835): Cleanup interpreter ''.
[Fri Jan 06 12:12:28.348954 2017] [wsgi:info] [pid 28835:tid 139790316758912] mod_wsgi (pid=28835): Terminating Python.
[Fri Jan 06 12:12:28.469333 2017] [wsgi:info] [pid 28835:tid 139790316758912] mod_wsgi (pid=28835): Python has shutdown.
[Fri Jan 06 12:12:28.469366 2017] [wsgi:info] [pid 28835:tid 139790316758912] mod_wsgi (pid=28835): Exiting process 'my_app'.
[Fri Jan 06 12:12:29.129042 2017] [wsgi:info] [pid 28372:tid 139790316758912] mod_wsgi (pid=28835): Process 'my_app' has died, deregister and restart it.
[Fri Jan 06 12:12:29.129094 2017] [wsgi:info] [pid 28372:tid 139790316758912] mod_wsgi (pid=28835): Process 'my_app' terminated normally, exit code 0
[Fri Jan 06 12:12:29.129099 2017] [wsgi:info] [pid 28372:tid 139790316758912] mod_wsgi (pid=28835): Process 'my_app' has been deregistered and will no longer be monitored.
[Fri Jan 06 12:12:29.129764 2017] [wsgi:info] [pid 28917:tid 139790316758912] mod_wsgi (pid=28917): Starting process 'my_app' with uid=33, gid=33 and threads=15.
[Fri Jan 06 12:12:29.130173 2017] [wsgi:info] [pid 28917:tid 139790316758912] mod_wsgi (pid=28917): Initializing Python.
[Fri Jan 06 12:12:29.144492 2017] [wsgi:info] [pid 28917:tid 139790316758912] mod_wsgi (pid=28917): Attach interpreter ''.
[Fri Jan 06 12:12:29.144687 2017] [wsgi:info] [pid 28917:tid 139790316758912] mod_wsgi (pid=28917): Adding '/var/anaconda2/lib/python2.7/site-packages' to path.
[Fri Jan 06 12:12:29.147279 2017] [wsgi:info] [pid 28917:tid 139790204262144] [remote 10.6.6.58:512] mod_wsgi (pid=28917, process='my_app', application=''): Loading WSGI script '/var/www/my_app/wsgi.py'.

2 个答案:

答案 0 :(得分:0)

看起来你错过了WSGIProcessGroup指令:

https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html#delegation-to-daemon-process

  

为在a中托管的WSGI应用程序使用守护进程模式   特定站点,WSGIDaemonProcess和WSGIProcessGroup指令   需要定义。例如,设置守护程序进程组   包含两个多线程进程可以使用:WSGIDaemonProcess   example.com processes = 2个threads = 15 WSGIProcessGroup example.com

这就是说,除非您需要调试的问题特定于您的部署配置,否则您最好使用builtin dev server

在本地工作

答案 1 :(得分:0)

由于在嵌入式模式下错误运行,您可能会看到更改,这可能意味着如果使用prefork MPM,新进程会获得请求。

除了正确设置嵌入模式(首选)之外,请阅读有关重新加载源代码的文档: