Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django中对同一视图的请求之间共享数据_Python_Django_Python 2.7 - Fatal编程技术网

Python 在django中对同一视图的请求之间共享数据

Python 在django中对同一视图的请求之间共享数据,python,django,python-2.7,Python,Django,Python 2.7,我在django有一个简单的视图 class SimpleView(View): def get(self, *args, **kwargs): c = C() c.m() class C: def m(self): x = # get from an external server if not set 我想在对SimpleView的不同请求中共享x 例如,让我们考虑下面的场景: 有人请求get到SimpleVie

我在django有一个简单的视图

class SimpleView(View):   
    def get(self, *args, **kwargs):   
        c = C()
        c.m()

class C:
    def m(self):
        x = # get from an external server if not set
我想在对
SimpleView的不同请求中共享
x

例如,让我们考虑下面的场景:

  • 有人请求
    get
    SimpleView
    <代码>x
  • 未设置,因此将从外部服务器获取

  • 某人2次请求
    get
    SimpleView
    x
    已设置,因此不会从外部服务器获取

  • 我知道那段代码做不到。我知道什么是全局变量。但是,我不知道如何在不同的请求之间共享数据。我不想使用数据库来存储它(因为优化)-值
    x
    不必是持久的

    请注意,如果共享某些内容的方法是线程安全的,那就太好了(这不是必需的,我会处理它)

    怎么做


    (django 1.6)

    由于不需要永久存储数据,因此应使用

    通常,会话引擎配置为使用数据库;可以使用其他后端,但很少有好的理由这样做


    (另外请注意,您应该升级您的Django版本;1.6版本很旧,不受支持,可能不安全。)

    谢谢!:),我还有两个问题:从我对
    session
    的基本理解来看,它不是由不同的客户共享的,可能我误解了session,是吗?另一个解决方案是什么?为什么要在客户之间共享值?因为
    x
    对于每个请求都是常见的。每次取东西都太贵了。