Django线程的全局数据 [英] Django global data for threads

查看:196
本文介绍了Django线程的全局数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单进程多线程django服务器中有一个共享的全局数据对象 - 一个经常使用的对象,但不经常计算。计算是耗时的,所以我想分享结果。



我以为可以使用django的LocalMemCache来实现这个简单的数据。奇怪的是,它似乎在单个页面加载时可以用于多个ajax调用,但由于某种原因,当我在浏览器中重新加载页面时,缓存再次为空。



我做错了什么?



有更好的方法吗?如果我使用线程锁控制写入访问,全局变量是否同样有效?



基本上我在做什么:

 从线程导入锁定
从django.core.cache导入get_cache

my_lock = Lock()
cache = get_cache('default')

def get_global_data():
my_lock.acquire()
try:
cached_data = cache.get('data')
如果不是cached_data:
cached_data = big_function_to_calculate_data()
cache.set('data',cached_data)
finally:
my_lock.release()
返回cached_data

#settings.py将我的django LocMemCache定义为:

CACHES = {
'default':{
'BACKEND':'django $ c


$ b

修改



根cau问题的根源是根据访问控制列表(不是代码的一部分)检索数据,这些数据根据请求类型(GET,POST)等而变化。在计算时,这是一个具有一组访问权限的POST请求,当读取它是一个具有不同访问权限的GET请求,并返回一组不同的(和无效的)结果。

解决方案

以上作品。作为一个附注,使用持久数据库缓存似乎比LocMemCache优先。

 #运行python manage.py createcachetable 

CACHES = {
'default':{
'BACKEND':'django.core.cache.backends.db.DatabaseCache',
'LOCATION':'my_cache_table '
}
}

问题的根本原因是检索数据在访问控制列表中,它根据请求类型(GET,POST)等而变化。在计算时,这是一个具有一组访问权限的POST请求,当读取它是具有不同访问权限的GET请求时。 / p>

I have a shared global data object in my single-process multi-threaded django server - an object which is frequently used, but calculated infrequently. The calculation is time-consuming, so I want to share the results.

I thought it would work to use django's LocalMemCache for this simple data. Oddly, it seems to work for multiple ajax calls on a single page load, but for some reason, when I reload the page in my browser, the cache is empty again.

What am I doing wrong?

Is there a better way? Would a global variable be just as efficient, if I control write-access with a thread lock?

Here's basically what I'm doing:

from threading import Lock
from django.core.cache import get_cache

my_lock = Lock()
cache = get_cache('default')

def get_global_data():
    my_lock.acquire()
    try:
        cached_data = cache.get('data')
        if not cached_data:
            cached_data = big_function_to_calculate_data()
            cache.set('data', cached_data)
    finally:
        my_lock.release()
    return cached_data

# settings.py defines my django LocMemCache as:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'my_server'
    }
}

Edit:

The root cause of the problem was retrieving data based on an access control list (not part of the code here), which varied based on request type (GET, POST), etc. When calculating, this was a POST request with one set of access, and when reading it was a GET request with a different set of access, and was returning a different (and invalid) set of results.

解决方案

The above works. As a side note, using a persistent database cache seems to be preferred over LocMemCache.

# run python manage.py createcachetable

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'my_cache_table'
    }
}

The root cause of the problem was retrieving data based on an access control list, which varied based on request type (GET, POST), etc. When calculating, this was a POST request with one set of access, and when reading it was a GET request with a different set of access.

这篇关于Django线程的全局数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆