Is it possible to use the python reload command (or similar) on a single module in a standalone cherrypy web app? I have a CherryPy based web application that often is under continual usage. From time to time I'll make an "important" change that only affects one module. I would like to be able to reload just that module immediately, without affecting the rest of the web application. A full restart is, admittedly, fast, however there are still several seconds of downtime that I would prefer to avoid if possible.
Asked
Active
Viewed 494 times
0
-
http://stackoverflow.com/questions/6270395/detect-if-a-python-module-changes-and-then-reload – Omid Raha Feb 13 '14 at 18:41
-
@OmidRaha close, but no cigar. That question has nothing to do with cherrypy. I know how to reload a module in general (see my reference to the python reload command), what I am looking for is a way to get CherryPy (not my code) to reload one of my modules. Unless it is as easy as calling reload in my code? – ibrewster Feb 13 '14 at 18:44
1 Answers
5
Reloading modules is very, very hard to do in a sane way. It leads to the potential of stale objects in your code with impossible-to-interrogate state and subtle bugs. It's not something you want to do.
What real web applications tend to do is to have a server that stays alive in front of their application, such as Apache with mod_proxy, to serve as a reverse proxy. You start your new app server, change your reverse proxy's routing, and only then kill the old app server.
No downtime. No insane, undebuggable code.

Mike Graham
- 73,987
- 14
- 101
- 130
-
http://tools.cherrypy.org/wiki/ModProxy is some information on using CherryPy with mod_proxy. (Feel free to use nginx or whatever instead of Apache as you prefer.) – Mike Graham Feb 13 '14 at 18:47
-
I see. So have two instances of my application server running (at least when modifying stuff). I am already using apache with mod_proxy, so this is entirely doable by simply using two different ports for the application server, and switching between the two. However, I would think I would have the same issue with restarting apache to change the proxy routing - the danger of a user not getting a response while apache was restarting to use the other server/port. Unless I can do a live update of the proxy path/routing? Hmmmm... – ibrewster Feb 13 '14 at 18:52
-
-
1Upvoted and want to add that uWSGI, a must-have server for Python apps, lets you [gracefully reload](http://uwsgi-docs.readthedocs.org/en/latest/articles/TheArtOfGracefulReloading.html) the code in several ways without losing client connections. – jwalker Feb 13 '14 at 20:47