One really easy way is CGI (together with a regular web server, and using wsgiref.handlers.CGIHandler
). Terrible for performance on a production server, but great for development. You can write a single script that works as both a mod_wsgi WSGIScriptAlias (exposing an application
object), and as a mod_cgi ScriptAlias (calling wsgiref
when __name__=='__main__'
).
Many WSGI environments have a way to reload the basic script, for example mod_wsgi's WSGIScriptReloading
, which is on by default. Unfortunately, you're likely to be putting much of your code in modules, which isn't so easy to reload. In mod_wsgi you can also do it by sending a SIGINT to perform a reload when in daemon mode. Unfortunately you still have to sniff every module you're using for mtime updates in order to know whether you have to reload. And it doesn't work in embedded mode.
A messy but feasible approach is to sniff all modules that are part of your application, and if any have been updated since the last check, reload them all. You have to reload them at once, by removing them all from the sys.modules
lookup (remove None
-valued entries too whilst you're there, to avoid relative import lookup problems), in order to ensure they don't keep cross-references to the old versions of themselves. And of course they must not leave other references to themselves outside of your application. You can see an example of this in action in the ModuleUpdater
class here.
(This software isn't ready for release, but has been providing module reloading for my WSGI apps for a few years and seems to be stable. The idea is to put all your WSGI app in an application class in a package, which you can import from a single WSGI/CGI/command-line entry point script; you include the deployment config in that script.)