Python Werkzeug中为什么需要绑定到上下文
我正在github中阅读库的源代码,在其中一个示例中(命名),在Python Werkzeug中为什么需要绑定到上下文,python,python-2.7,werkzeug,Python,Python 2.7,Werkzeug,我正在github中阅读库的源代码,在其中一个示例中(命名),在application.py文件中,有一个函数将应用程序绑定到当前活动上下文。我想知道为什么这是必要的,或者我在哪里可以找到解释这一点的东西 其功能是: def bind_to_context(self): """ Useful for the shell. Binds the application to the current active context. It's auto
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
这是dispatcher绑定请求的部分
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()
据我所知,
Werkzeug
中的context
是关于在不同线程之间分离环境的。例如,上下文在Flask
框架中非常常见,该框架构建在Werkzeug
之上。您可以在多线程模式下运行Flask应用程序。在这种情况下,您将只有一个由多个线程同时访问的应用程序对象。每个线程都需要应用程序中的一段数据供私人使用。存储此类数据是通过以下方式组织的。这就是上下文