WSGI ( is caching mysql result until script code i

2020-05-03 10:25发布

问题:

This is the basic wsgi code.

import MySQLdb
conn = MySQLdb.connect (host = "localhost",
                        user = "root",
                        passwd = "",
                        db = "a")
cursor = conn.cursor ()
cursor.execute ("select * from `01` where id in (1,2) limit 2")
rows = cursor.fetchall()
cursor.close ()
conn.close ()

test = rows[0][1]
test2 = rows[1][1]

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html')])
    yield test

the problem here is the mysql result is being cached.. it is not mysql caching it.. i suspect it is this script doing it.

i would like a solution to stop this unwanted caching.

currently the only way to clear the caching seems to be to modify the script code above.. as in.. simply updating it seems to do the trick..

but i can not just update it 24/7.. there has to be a way to turn this caching off.

should i add a rand() number to the mysql query.. would that solve the problem ? obviously something is caching the mysql results simply because either

a) the mysql query is same as something previously executed
b) the script has not been modified ever since it executed a mysql query ?

回答1:

Anything at module level is executed once, when the script is first run or imported. Your application function always yields the same result, the one that was calculated on first run.

As Omid says in the comments, you need to put the SQL code in a function that is called from the application function.