简体   繁体   中英

Python code in Django not recompiling in daemon mode with Apache/mod_wsgi

I am running a Django application using Apache2 with mod_wsgi in daemon mode:

WSGIApplicationGroup %{GLOBAL}

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

Whenever I make changes to my Python code on server (I know this is highly discouraged, but I'm only using it for debugging) I keep having trouble with recompiling the code, but only in subdirectories.

Python files in the root directory of the application (ie settings.py) get recompiled nicely, but not the ones located in subdirectories (ie views.py of some app).

The application directory is owned by the www-data user and the user has sufficient privileges (tested with 777).

Restarting the Apache or touching the wsgi file only results in recompiling of the Python files in root directory of the application.

EDIT:

I also tried adding the WSGIProcessGroup directive with no effect:

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

Also, using the code provided here I have concluded I am, in fact, running in daemon mode.

EDIT #2

Added Apache log from restart:

[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'.

Added Apache log from touching the WSGI script:

[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'.

It looks like your missing the WSGIProcessGroup directive:

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

To make use of daemon mode for WSGI applications hosted within a specific site, the WSGIDaemonProcess and WSGIProcessGroup directives would need to be defined. For example, to setup a daemon process group containing two multithreaded process one could use: WSGIDaemonProcess example.com processes=2 threads=15 WSGIProcessGroup example.com

This being said, unless the issues you need to debug are specific to your deployment configuration, you'd be better working locally with the builtin dev server

It would be by chance that you are seeing changes due to wrongly running in embedded mode, which probably means new process gets request if using prefork MPM.

In addition to properly setting up embedded mode, which is is preferred, read the documentation about source code reloading at:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM