Web.py - prevent caching of code/output

342 views Asked by At

This problem is baffling because the behavior is quite inconsistent.

I am learning about writing apps for the Web.py framework. For example, I have the following structure:

web_app/
 |-- main.py
 |-- api_calls
 |    |-- __init__.py
 |    |-- test1.py
 |    |-- test2.py
 \----\-- common.py

I call spawn-fcgi to start my web app as follows: spawn-fcgi -s /run/webapp.sock -U http -G http /srv/http/web_app/main.py.

Now, since I'm still learning how to write web apps this way, I am frequently updating the code. I have some functions in common.py which all of the "API files" may call, so they import common. In short, all of the import logic I expect to work does in fact work.

The problem is that if I change a piece of code, it seems highly inconsistent whether the web server, the fcgi process, or Python itself, detects this change and responds by executing the new code. For example. if I make a change in common.py, then other files which call it still reference the old code. However, if I make a change in test1.py or main.py then these changes may appear immediately upon a page reload. Again, though, even this seems inconsistent - sometimes it doesn't work.

I found that touching all of the .py files sometimes works to force the server to update, but again not always.

The only surefire way it seems to get new code to run consistently is to:

  1. kill the Python process the spawn-fcgi program started
  2. touch all .py files
  3. remove all .pyc files
  4. restart the spawn-fcgi daemon.

My question is two-fold: Can someone explain how the combination of Python, web.py, spawn-fcgi, and nginx handles code and page response caching? And secondly, is what I specified the only reliable way to ensure that any change I make to the code will be noticed when I refresh the webpage to see my changes? (Right now I am manually executing ps to find the correct Python process as I have other Python daemons running, then using find to touch .py and remove .pyc files, and then manually re-entering the spawn-fcgi command I indicated above - a tedious procedure when I may have only changed a single line of code...)

2

There are 2 answers

0
fdmillion On

I ended up having to write a small script alias to manually kill the fcgi process, delete the .pyc files, and restart the fcgi process. It works, but it's certainly not elegant...

alias newcode='kill `cat /run/fcgi-api.pid` ; sleep 0.5 && rm -f /mnt/data/www/api/*.pyc /run/fcgi-api.sock && spawn-fcgi -P /run/fcgi-api.pid -U http -G http -s /run/fcgi-api.sock /mnt/data/www/api/api.py'
0
user3067740 On

I had the same issue! web.py was giving cached results from old code. I had to restart Apache to prevent this from happening using apache2ctl restart.