Django: Unexpectedly persistent module variables
Question: I noticed a strange behaviour today: It seems that, in the following example,
the config.CLIENT variable stays persistent accross requests – even if the
view gets passed an entirely different client_key, the query that gets the
client is only executed once (per many requests), and then the config.CLIENT
variable stays assigned.

It does not seem to be a database caching issue.

It happens with mod_python as well as with the test server (the variable is
reassigned when the test server is restarted).

What am I missing here?

    
    
    #views.py
    from my_app import config
    
    def get_client(client_key=None):
        if config.CLIENT == None:
            config.CLIENT = get_object_or_404(Client, key__exact=client_key, is_active__exact=True)
        return config.CLIENT
    
    def some_view(request, client_key):
        client = get_client(client_key)
        ...
        return some_response
    
    # config.py
    CLIENT = None
    


Answer: Multiple requests are processed by the same process and global variables like
your CLIENT live as long, as process does. You shouldn't rely on global
variables, when processing requests - use either local ones, when you need to
keep a variable for the time of building response or put data into the
database, when something must persist across multiple requests.

If you need to keep some value through the request you can either add it to
thread locals [(here you should some examples, that adds user info to
locals)](http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser) or
simply pass it as a variable into other functions.

