Hanfei Sun
Hanfei Sun

Reputation: 47051

Use cache under mod_wsgi for Python web framework

The codes are like this(I'm using flask and flask-cache but this might be a general problem):

@cache.memoize(500000)
def big_foo(a,b):
    return a + b + random.randrange(0, 1000)

If I run it in a Python intepreter, I can always get the same result by calling big_foo(1,2).

But if I add this function in the application and use mod_wsgi to daemon, then request in browser. (the big_foo is called within the views function of that request). I found the result is not the same each time.

I think the results are different each time because mod_wsgi use multiple process to launch the app. Each process might have their own cache, and the cache can't be shared between process.

Is my guess right? If right, how can I assign one and only one cache for global accessing? If not, where was wrong with my codes?

Following is the config used for flask-cache

UPLOADS_FOLDER = "/mnt/Storage/software/x/temp/"                                                                                 
class RadarConfig(object):                                                                                                                   
    UPLOADS_FOLDER = UPLOADS_FOLDER                                                                                                          
    ALLOWED_EXTENSIONS = set(['bed'])                                                                                                        
    SECRET_KEY = "tiananmen"                                                                                                                 
    DEBUG = True                                                                                                                             
    CACHE_TYPE = 'simple'                                                                                                                    
    CACHE_DEFAULT_TIMEOUT = 5000000                                                                                                          
    BASIS_PATH = "/mnt/Storage/software/x/NMF_RESULT//p_NMF_Nimfa_NMF_Run_30632__metasites_all"                              
    COEF_PATH = "/mnt/Storage/software/x/NMF_RESULT/MCF7/p_NMF_Nimfa_NMF_Run_30632__metasample_all"                              
    MASK_PATH = "/mnt/Storage/software/x/NMF_RESULT/dhsHG19.bed"    

Upvotes: 0

Views: 883

Answers (1)

Audrius Kažukauskas
Audrius Kažukauskas

Reputation: 13543

Here's your problem: CACHE_TYPE = 'simple'. From SimpleCache documentation:

Simple memory cache for single process environments. This class exists mainly for the development server and is not 100% thread safe.

For production better suited backends are memcached, redis and filesystem, since they're designed to work in concurrent environments.

Upvotes: 4

Related Questions