【问题标题】:Why binding to context is necessary in Werkzeug为什么在 Werkzeug 中需要绑定到上下文
【发布时间】:2016-02-02 12:38:53
【问题描述】:

我正在阅读 github 中 Werkzeug 库的源代码,并且在其中一个示例(命名为 Simplewiki)中,application.py 文件中有一个函数将应用程序绑定到当前活动上下文.我想知道为什么这是必要的,或者我在哪里可以找到解释这一点的东西?

函数是这样的:

def bind_to_context(self):
        """
        Useful for the shell.  Binds the application to the current active
        context.  It's automatically called by the shell command.
        """
        local.application = self

这就是调度程序绑定请求的部分。

def dispatch_request(self, environ, start_response):
        """Dispatch an incoming request."""
        # set up all the stuff we want to have for this request.  That is
        # creating a request object, propagating the application to the
        # current context and instanciating the database session.
        self.bind_to_context()
        request = Request(environ)
        request.bind_to_context()

【问题讨论】:

    标签: python python-2.7 werkzeug


    【解决方案1】:

    据我所知,Werkzeug 中的contexts 是关于在不同线程之间分离环境。例如,上下文在Flask 框架中非常常见,该框架构建在Werkzeug 之上。您可以在多线程模式下运行 Flask 应用程序。在这种情况下,您将只有一个由多个线程同时访问的应用程序对象。每个线程都需要应用程序中的一段数据供私人使用。通过thread's local storage 组织存储此类数据。这就是所谓的上下文。

    【讨论】:

      猜你喜欢
      • 2016-02-11
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      相关资源
      最近更新 更多