最近一直在研究Flask,由于gfirefly中提供的Http接口使用了Flask,以前都是写一些游戏中简单的操作,最近涉及到Flask的方面比较多,所以就认真研究了下。对Flask的request context和app context略有心得,所以和小伙伴们分享一下Flask的request原理。
在我们视图中要使用request时只需要from flask import request就可以了
很好奇在多线程的环境下,是如何保证request没有混乱的
在flask.globals.py中
- def _lookup_req_object(name):
- top = _request_ctx_stack.top
- if top is None:
- raise RuntimeError('working outside of request context')
- return getattr(top, name)
- _request_ctx_stack = LocalStack()
- request = LocalProxy(partial(_lookup_req_object, 'request'))
- session = LocalProxy(partial(_lookup_req_object, 'session'))
对象同时保持request和session。我们只要一处导入request,在任何视图函数中都可以使用request,
关键是每次的都是不同的request对象,说明获取request对象肯定是一个动态的操作,不然肯定都是相同的request。这里的魔法就是_lookup_req_object函数和LocalProxy组合完成的。
LocalProxy是werkzeug.local.py中定义的一个代理对象,它的作用就是将所有的请求都发给内部的_local对象
- class LocalProxy(object):
- def __init__(self, local, name=None):
- #LocalProxy的代码被我给简化了,这里的local不一定就是local.py中定义的线程局部对象,也可以是任何可调用对象
- #在我们的request中传递的就是_lookup_req_object函数
- object.__setattr__(self, '_LocalProxy__local', local)
- object.__setattr__(self, '__name__', name)
- def _get_current_object(self):
- #很明显,_lookup_req_object函数没有__release_local__
- if not hasattr(self.__local, '__release_local__'):
- return self.__local()
- try:
- return getattr(self.__local, self.__name__)
- except AttributeError:
- raise RuntimeError('no object bound to %s' % self.__name__)
- def __getattr__(self, name):
- return getattr(self._get_current_object(), name)
既然每次request都不同,要么调用top = _request_ctx_stack.top返回的top不同,要么top.request属性不同,在flask中每次返回的top是不一样的,所以request的各个属性都是变化的。
现在需要看看_request_ctx_stack = LocalStack(),LocalStack其实就是简单的模拟了堆栈的基本操作,push,top,pop,内部保存的线程本地变量是在多线程中request不混乱的关键。
- class Local(object):
- __slots__ = ('__storage__', '__ident_func__')
- def __init__(self):
- object.__setattr__(self, '__storage__', {})
- object.__setattr__(self, '__ident_func__', get_ident)
- def __getattr__(self, name):
- return self.__storage__[self.__ident_func__()][name]
下面我们来看看整个交互过程,_request_ctx_stack堆栈是在哪里设置push的,push的应该是我们上面说的同时具有request和session属性的对象,那这家伙又到底是什么?
flask从app.run()开始
- class Flask(_PackageBoundObject):
- def run(self, host=None, port=None, debug=None, **options):
- from werkzeug.serving import run_simple
- run_simple(host, port, self, **options)
在run_wsgi的run_wsgi我们可以清晰的看到调用过程
- def run_wsgi(self):
- environ = self.make_environ()
- def start_response(status, response_headers, exc_info=None):
- if exc_info:
- try:
- if headers_sent:
- reraise(*exc_info)
- finally:
- exc_info = None
- elif headers_set:
- raise AssertionError('Headers already set')
- headers_set[:] = [status, response_headers]
- return write
- def execute(app):
- application_iter = app(environ, start_response)
- #environ是为了给request传递请求的
- #start_response主要是增加响应头和状态码,最后需要werkzeug发送请求
- try:
- for data in application_iter: #根据wsgi规范,app返回的是一个序列
- write(data) #发送结果
- if not headers_sent:
- write(b'')
- finally:
- if hasattr(application_iter, 'close'):
- application_iter.close()
- application_iter = None
- try:
- execute(self.server.app)
- except (socket.error, socket.timeout) as e:
- pass
- class Flask(_PackageBoundObject):
- def __call__(self, environ, start_response):
- """Shortcut for :attr:`wsgi_app`."""
- return self.wsgi_app(environ, start_response)
- def wsgi_app(self, environ, start_response):
- ctx = self.request_context(environ)
- #这个ctx就是我们所说的同时有request,session属性的上下文
- ctx.push()
- error = None
- try:
- try:
- response = self.full_dispatch_request()
- except Exception as e:
- error = e
- response = self.make_response(self.handle_exception(e))
- return response(environ, start_response)
- finally:
- if self.should_ignore_error(error):
- error = None
- ctx.auto_pop(error)
- def request_context(self, environ):
- return RequestContext(self, environ)
我们看看ctx.push
- class RequestContext(object):
- def __init__(self, app, environ, request=None):
- self.app = app
- if request is None:
- request = app.request_class(environ) #根据环境变量创建request
- self.request = request
- self.session = None
- def push(self):
- _request_ctx_stack.push(self) #将ctx push进 _request_ctx_stack
- # Open the session at the moment that the request context is
- # available. This allows a custom open_session method to use the
- # request context (e.g. code that access database information
- # stored on `g` instead of the appcontext).
- self.session = self.app.open_session(self.request)
- if self.session is None:
- self.session = self.app.make_null_session()
- def pop(self, exc=None):
- rv = _request_ctx_stack.pop()
- def auto_pop(self, exc):
- if self.request.environ.get('flask._preserve_context') or \
- (exc is not None and self.app.preserve_context_on_exception):
- self.preserved = True
- self._preserved_exc = exc
- else:
- self.pop(exc)
我们看到ctx.push操作将ctx push到_request_ctx_stack,所以当我们调用request.method时将调用_lookup_req_object。 top此时就是ctx上下文对象,而getattr(top, "request")将返回ctx的request,而这个request就是在ctx的__init__中根据环境变量创建的。
哈哈,明白了吧,每一次调用视图函数操作之前,flask会把创建好的ctx放在线程Local中,当使用时根据线程id就可以拿到了。在wsgi_app的finally中会调用ctx.auto_pop(error),会根据情况判断是否清除放_request_ctx_stack中的ctx。
上面是我简化的代码,其实在RequestContext push中_app_ctx_stack = LocalStack()是None,也会把app push进去,对应的app上下文对象为AppContext。
我们知道flask还有一个神秘的对象g,flask从0.10开始g是和app绑定在一起的(http://flask.pocoo.org/docs/0.10/api/#flask.g),g是AppContext的一个成员变量。虽然说g是和app绑定在一起的,但不同请求的AppContext是不同的,所以g还是不同。也就是说你不能再一个视图中设置g.name,然后再另一个视图中使用g.name,会提示AttributeError。
绑定要app的好处就是可以脱离request使用g,否则就要使用flask._app_ctx_stack.top代替g,
可参考http://dormousehole.readthedocs.org/en/latest/patterns/sqlite3.html
到这里,不知道你对RequestContext还有没有疑惑,这里有一点比较疑惑的是我们当前request只有一个,为什么要request要使用栈,而不是直接保存当前的request实例?
其实这主要是为了多app共存考虑的,由于我们一般都只有一个app,所以栈顶存放的肯定是当前request对象,当如果是多个app,那么栈顶存放的是当前活跃的request,也就是说使用栈是为了获取当前的活跃request对象。
下面使用gtwisted模拟多个app,由于gtwisted使用了greenlet,所以多个app.run并不会阻塞,当然你也可以使用线程来模拟。
- from flask import Flask, url_for
- from gtwisted.core import reactor
- app1 = Flask("app1")
- app2 = Flask("app2")
- @app1.route("/index1")
- def index1():
- return "app1"
- @app1.route("/home")
- def home():
- return "app1home"
- @app2.route("/index2")
- def index2():
- return "app2"
- # reactor.listenWSGI(8000, app1)
- # reactor.listenWSGI(8001, app2)
- # reactor.run()
- with app1.test_request_context():
- print url_for('index1')
- with app2.test_request_context():
- print url_for('index2')
- print url_for('home')
如果将中间三行去掉,可以通过浏览器127.0.0.1:8000/index1,127.0.0.1:80001/index2访问多个app
输出结果:
当app1.test_request_context()时,会将app1对应的context压栈,当前活跃app context是app1的,所以url_for使用的是app1的context,当with app2.test_request_context()时,会将app2对应的context压栈,当前活跃app context是app2的,当with过了作用域,将弹出app2的context,此时活跃的为app1的,这就是为什么context使用栈而不是实例变量。
到这里各位小伙伴们都明白了吧,flask以优雅的方式给我们提供了很大的便利,自己做了很多的工作,这就是当代活雷锋!!!
最近一直在研究Flask,由于gfirefly中提供的Http接口使用了Flask,以前都是写一些游戏中简单的操作,最近涉及到Flask的方面比较多,所以就认真研究了下。对Flask的request context和app context略有心得,所以和小伙伴们分享一下Flask的request原理。
在我们视图中要使用request时只需要from flask import request就可以了
很好奇在多线程的环境下,是如何保证request没有混乱的
在flask.globals.py中
- def _lookup_req_object(name):
- top = _request_ctx_stack.top
- if top is None:
- raise RuntimeError('working outside of request context')
- return getattr(top, name)
- _request_ctx_stack = LocalStack()
- request = LocalProxy(partial(_lookup_req_object, 'request'))
- session = LocalProxy(partial(_lookup_req_object, 'session'))
对象同时保持request和session。我们只要一处导入request,在任何视图函数中都可以使用request,
关键是每次的都是不同的request对象,说明获取request对象肯定是一个动态的操作,不然肯定都是相同的request。这里的魔法就是_lookup_req_object函数和LocalProxy组合完成的。
LocalProxy是werkzeug.local.py中定义的一个代理对象,它的作用就是将所有的请求都发给内部的_local对象
- class LocalProxy(object):
- def __init__(self, local, name=None):
- #LocalProxy的代码被我给简化了,这里的local不一定就是local.py中定义的线程局部对象,也可以是任何可调用对象
- #在我们的request中传递的就是_lookup_req_object函数
- object.__setattr__(self, '_LocalProxy__local', local)
- object.__setattr__(self, '__name__', name)
- def _get_current_object(self):
- #很明显,_lookup_req_object函数没有__release_local__
- if not hasattr(self.__local, '__release_local__'):
- return self.__local()
- try:
- return getattr(self.__local, self.__name__)
- except AttributeError:
- raise RuntimeError('no object bound to %s' % self.__name__)
- def __getattr__(self, name):
- return getattr(self._get_current_object(), name)
既然每次request都不同,要么调用top = _request_ctx_stack.top返回的top不同,要么top.request属性不同,在flask中每次返回的top是不一样的,所以request的各个属性都是变化的。
现在需要看看_request_ctx_stack = LocalStack(),LocalStack其实就是简单的模拟了堆栈的基本操作,push,top,pop,内部保存的线程本地变量是在多线程中request不混乱的关键。
- class Local(object):
- __slots__ = ('__storage__', '__ident_func__')
- def __init__(self):
- object.__setattr__(self, '__storage__', {})
- object.__setattr__(self, '__ident_func__', get_ident)
- def __getattr__(self, name):
- return self.__storage__[self.__ident_func__()][name]
下面我们来看看整个交互过程,_request_ctx_stack堆栈是在哪里设置push的,push的应该是我们上面说的同时具有request和session属性的对象,那这家伙又到底是什么?
flask从app.run()开始
- class Flask(_PackageBoundObject):
- def run(self, host=None, port=None, debug=None, **options):
- from werkzeug.serving import run_simple
- run_simple(host, port, self, **options)
在run_wsgi的run_wsgi我们可以清晰的看到调用过程
- def run_wsgi(self):
- environ = self.make_environ()
- def start_response(status, response_headers, exc_info=None):
- if exc_info:
- try:
- if headers_sent:
- reraise(*exc_info)
- finally:
- exc_info = None
- elif headers_set:
- raise AssertionError('Headers already set')
- headers_set[:] = [status, response_headers]
- return write
- def execute(app):
- application_iter = app(environ, start_response)
- #environ是为了给request传递请求的
- #start_response主要是增加响应头和状态码,最后需要werkzeug发送请求
- try:
- for data in application_iter: #根据wsgi规范,app返回的是一个序列
- write(data) #发送结果
- if not headers_sent:
- write(b'')
- finally:
- if hasattr(application_iter, 'close'):
- application_iter.close()
- application_iter = None
- try:
- execute(self.server.app)
- except (socket.error, socket.timeout) as e:
- pass
- class Flask(_PackageBoundObject):
- def __call__(self, environ, start_response):
- """Shortcut for :attr:`wsgi_app`."""
- return self.wsgi_app(environ, start_response)
- def wsgi_app(self, environ, start_response):
- ctx = self.request_context(environ)
- #这个ctx就是我们所说的同时有request,session属性的上下文
- ctx.push()
- error = None
- try:
- try:
- response = self.full_dispatch_request()
- except Exception as e:
- error = e
- response = self.make_response(self.handle_exception(e))
- return response(environ, start_response)
- finally:
- if self.should_ignore_error(error):
- error = None
- ctx.auto_pop(error)
- def request_context(self, environ):
- return RequestContext(self, environ)
我们看看ctx.push
- class RequestContext(object):
- def __init__(self, app, environ, request=None):
- self.app = app
- if request is None:
- request = app.request_class(environ) #根据环境变量创建request
- self.request = request
- self.session = None
- def push(self):
- _request_ctx_stack.push(self) #将ctx push进 _request_ctx_stack
- # Open the session at the moment that the request context is
- # available. This allows a custom open_session method to use the
- # request context (e.g. code that access database information
- # stored on `g` instead of the appcontext).
- self.session = self.app.open_session(self.request)
- if self.session is None:
- self.session = self.app.make_null_session()
- def pop(self, exc=None):
- rv = _request_ctx_stack.pop()
- def auto_pop(self, exc):
- if self.request.environ.get('flask._preserve_context') or \
- (exc is not None and self.app.preserve_context_on_exception):
- self.preserved = True
- self._preserved_exc = exc
- else:
- self.pop(exc)
我们看到ctx.push操作将ctx push到_request_ctx_stack,所以当我们调用request.method时将调用_lookup_req_object。 top此时就是ctx上下文对象,而getattr(top, "request")将返回ctx的request,而这个request就是在ctx的__init__中根据环境变量创建的。
哈哈,明白了吧,每一次调用视图函数操作之前,flask会把创建好的ctx放在线程Local中,当使用时根据线程id就可以拿到了。在wsgi_app的finally中会调用ctx.auto_pop(error),会根据情况判断是否清除放_request_ctx_stack中的ctx。
上面是我简化的代码,其实在RequestContext push中_app_ctx_stack = LocalStack()是None,也会把app push进去,对应的app上下文对象为AppContext。
我们知道flask还有一个神秘的对象g,flask从0.10开始g是和app绑定在一起的(http://flask.pocoo.org/docs/0.10/api/#flask.g),g是AppContext的一个成员变量。虽然说g是和app绑定在一起的,但不同请求的AppContext是不同的,所以g还是不同。也就是说你不能再一个视图中设置g.name,然后再另一个视图中使用g.name,会提示AttributeError。
绑定要app的好处就是可以脱离request使用g,否则就要使用flask._app_ctx_stack.top代替g,
可参考http://dormousehole.readthedocs.org/en/latest/patterns/sqlite3.html
到这里,不知道你对RequestContext还有没有疑惑,这里有一点比较疑惑的是我们当前request只有一个,为什么要request要使用栈,而不是直接保存当前的request实例?
其实这主要是为了多app共存考虑的,由于我们一般都只有一个app,所以栈顶存放的肯定是当前request对象,当如果是多个app,那么栈顶存放的是当前活跃的request,也就是说使用栈是为了获取当前的活跃request对象。
下面使用gtwisted模拟多个app,由于gtwisted使用了greenlet,所以多个app.run并不会阻塞,当然你也可以使用线程来模拟。
- from flask import Flask, url_for
- from gtwisted.core import reactor
- app1 = Flask("app1")
- app2 = Flask("app2")
- @app1.route("/index1")
- def index1():
- return "app1"
- @app1.route("/home")
- def home():
- return "app1home"
- @app2.route("/index2")
- def index2():
- return "app2"
- # reactor.listenWSGI(8000, app1)
- # reactor.listenWSGI(8001, app2)
- # reactor.run()
- with app1.test_request_context():
- print url_for('index1')
- with app2.test_request_context():
- print url_for('index2')
- print url_for('home')
如果将中间三行去掉,可以通过浏览器127.0.0.1:8000/index1,127.0.0.1:80001/index2访问多个app
输出结果:
当app1.test_request_context()时,会将app1对应的context压栈,当前活跃app context是app1的,所以url_for使用的是app1的context,当with app2.test_request_context()时,会将app2对应的context压栈,当前活跃app context是app2的,当with过了作用域,将弹出app2的context,此时活跃的为app1的,这就是为什么context使用栈而不是实例变量。
到这里各位小伙伴们都明白了吧,flask以优雅的方式给我们提供了很大的便利,自己做了很多的工作,这就是当代活雷锋!!!