13

Does anyone know how to make a mod_wsgi automatically reload a Flask app when any of the modules changes? I've tried WSGIScriptReloading On, but no luck. The official documentation is kind of a bear ... I guess I'll give it a stab if no one knows. Thanks in advance!

Also, if it could not permanently crash on syntax errors (like the the Flask reloader), that'd be awesome.

6 Answers 6

20

With mod_wsgi, WSGIScriptReloading looks for changes to the .wsgi config file, rather than the code.

My workflow is to upload my code changes then just

$ touch MyWebApp.wsgi

which causes the last modified file timestamp to change and mod_wsgi to reload the code.

You can do this 'remotely' by saving the .wsgi file on your local machine and then uploading it again, or I just do it via SSH.

Not a lot you can do about syntax errors, the code is either running or it isn't, but a fix plus a touch will get it running again.

One gotcha to look out for if you're working via FTP: make sure you upload the 'touched' .wsgi file last otherwise it'll try and start with the wrong code.

6
  • 2
    Should be note that what you are talking about only works for mod_wsgi daemon mode. Dec 7, 2012 at 10:25
  • @GrahamDumpleton is this an acceptable workflow with mod_wsgi, in your opinion?
    – johnny
    Apr 24, 2015 at 17:19
  • If you do not have the ability to easily restart the Apache web server itself, this is the easiest approach. Apr 24, 2015 at 20:57
  • Thanks, this works. I now keep this running while developing locally: $ while true; do touch MyWebApp.wsgi ; sleep 1; done (loop to touch the file every second)
    – Presari
    Sep 10, 2017 at 14:34
  • @GrahamDumpleton how to run it in daemon mode? Isn't it the default?
    – Basj
    Apr 20, 2018 at 21:15
1

I think it's a very realistic situation to want to reload source code automatically in production. Think of an environment where sources are deployed per version and a 'production' symlink points to one of those versions. Whenever you want release a newer version, you just point the symlink to another path. But apache and mod_wsgi still collect the files from the symlinked directory and therefor need to have a reloading mechanism in place based on timestamps, size or w/e. Sure, one application might not be a problem, but what about hosting 15-20 applications that are all undergoing active development? Not automatically reloading sources is a pure loss in such a situation compared to restarting apache every single time.

Back to the question: if the framework you're using (in this case flask) does not have a plugin or tool in place for automatic source code reloading, then the two options described by Graham and Malphas are your best options. Either trigger the wsgi process to restart or implement a monitoring system.

1

You're right about adding the WSGIScriptReloading directive. The Flask docs don't make it 100% clear, but Apache looks for changes to your .wsgi file. The recommended solution is to just execute a touch command on your your .wsgi file, as part of your release process.

0

For production I prefer apache mod_wsgi too, while for development I use the flask's built-in server. I separate prod and dev config files and I set the debug directive True in the dev config so that Flask can detect and reload code changes automatically.

1
  • Consider looking at using mod_wsgi-express for development. Check out my blog for recent posts about it and look for upcoming ones which talk more about using it in a development environment. Apr 17, 2015 at 10:46
-1

The correct answer to this question is that the WSGIScriptReloading On needs to be added into 000-default.conf present under /etc/apache2/sites-enabled folder.

See below example

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerName sentiments.live
        ServerAdmin [email protected]
        DocumentRoot /var/www/html
        WSGIDaemonProcess flaskapp threads=5
        WSGIScriptAlias / /var/www/html/sentiments/flaskapp.wsgi

        <Directory sentiments>
                WSGIScriptReloading On
                WSGIProcessGroup sentiments
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
4
  • You do not need WSGIScriptReloading On as that is the default setting. For further information see modwsgi.readthedocs.io/en/develop/user-guides/… Mar 7, 2018 at 7:43
  • You also have a number of other things wrong. The argument to Directory should be a absolute path starting with / else what it matches is uncertain and depends on global Apache setup. Mar 7, 2018 at 7:44
  • The argument to WSGIProcessGroup also doesn't match the name you used for WSGIDaemonProcess, so would be failing or matching wrong daemon process group if there was one elsewhere of that name defined. Mar 7, 2018 at 7:45
  • Finally, you should not put your source code under the directory specified by DocumentRoot as a stuff up in other configuration could then result in your source code being able to be downloaded. Put your source code in a different directory. Mar 7, 2018 at 7:45
-2

What do you mean 'the official documentation is kind of a bear'? What is wrong with the included recipe:

That document also explains why WSGIScriptReloading doesn't do what you expect.

And no it is not possible to permanently crash on syntax errors. It is embedded in Apache and the whole point of Apache is to keep stuff running.

Sounds like you should not be using Apache/mod_wsgi for development. Everyones knows one should not use automatic source code reloading in production so can't imagine you would want to do that.

3
  • 1
    I tend to think 'the official documentation is kind of a bear' just is a disclaimer against whiner comments on SO for "RTFM" first. Dec 7, 2012 at 10:23
  • Oh, he means that no one wants to spend 2-3 hours reading about automatic source code reloading anymore. Sep 17, 2013 at 20:50
  • 1
    More that no one can be bothered reading and understanding documentation full stop these days. They expect people on SO to do the research for them and solve all their problems. What they don't realise is that they are destroying the support network that is available to help out as the original package authors can't be bothered to help users any more because the users don't want to try and help themselves first. Sep 18, 2013 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.