Subsections
The development cycle of a CherryPy web site is the same as the development cycle of a regular software:
- Write source files
- Compile them
- If compiler generated some errors, fix source files and try to compile again
- Once compilation was OK, stop running executable and restart the newly generated executable
- If we find some bugs or we want some new features, modify the source files and go back to the compilation stage
The compilation process is usually pretty fast (if you have a "decent" computer).
But what if the CherryPy server has a long initialization process (maybe it needs to connect to several ressources,
like databases, or it needs to precompute some data). All this work is usually done in the initServer special
function. If this initialization takes a lot of time, having to stop and restart the server everytime you make
a change is really a loss of time while developing your web site.
This is where the hotReload feature comes in. It allows you to refresh your code without having to stop
the server.
When it receives a hotReload request, the server will just read its source code again, updating all the CherryClasses
code and recreating new CherryClasses instances with the new code. It will not execute initServer again. Instead,
it will execute hotReloadInitServer if any.
The hotReload feature is enabled in the executable when the file is compiled with the -D flag (debug mode).
To send a hotReload request to a running CherryPy server, just use the little script called cherryhotreload.py
provided with the distribution.
Let's take a practical example that shows how to use the hotReload feature:
- You write a first version of your source file called Root.cpy
- You compile your source file in debug mode:
python ../cherrypy.py -D Root.cpy
- In a window, you start the CherryPy server:
- Now you have a running CherryPy server
- You realize that you want to change something in your code
- Just update your source file and recompile it (in another window, since the server is still running in the first window)
- Send the hotReload request to the running server to tell it to refresh its code:
python cherryhotreload.py localhost:8080
(This assumes that the server is running on localhost:8080. Of course, you have to adapt it for your own configuration)
- In the window where the server is running, you see a message saying that it has performed the hotReload
Warning: After a hotReload, the line numbers that the python interpreter might give you in case of an error
may not match your source file. This happens if you add or remove lines of code before the hotReload
See About this document... for information on suggesting changes.