开发者

Django: Unexpectedly persistent module variables

开发者 https://www.devze.com 2022-12-16 07:27 出处:网络
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 di

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


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) or simply pass it as a variable into other functions.


OK, just to make it slightly clearer (and in response to the comment by Felix), I'm posting the code that does what I needed. The whole problem arose from a fundamental misunderstanding on my part and I'm sorry for any confusion I might have caused.

import config

# This will be called once per request/view
def init_client(client_key):
    config.CLIENT = get_object_or_404(Client, key__exact=client_key, is_active__exact=True)

# This might be called from other modules that are unaware of requests, views etc 
def get_client():
    return config.CLIENT
0

精彩评论

暂无评论...
验证码 换一张
取 消