码迷,mamicode.com
首页 > 其他好文 > 详细

Flask系列之源码分析(二)

时间:2018-04-26 19:52:05      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:blog   nal   pat   current   read   got   ali   hat   object   

应用技术点

flask源码上下文管理

1、综述过程

将请求对象压入栈

1.请求进入

__call__ ---> wsgi_app ---> ctx = self.request_context(environ)

初始化请求对象

2.通过ctx.puth()建立2个请求堆栈(采用threading.local)

  --app_ctx(app,g)

  --ctx(request,session)

3._implicit_app_ctx_stack初始化

4._request_ctx_stack初始化

从栈中将请求对象调取出来

1.经过层层建立到达视图函数

  --request

  --session

  --current_app

  --g

2.以上四个对象通过localProxy(采用threading.local),调用

  --_lookup_app_object

  --_find_app

  --_lookup_app

3.以上三个方法调用

  _lookup_req_object ----> _implicit_app_ctx_stack

  _find_app和_lookup_app  ----> _request_ctx_stack

2、将请求对象压入栈

1.请求进入

__call__ ---> wsgi_app ---> ctx = self.request_context(environ)

初始化请求对象

wsgi_app源码

# Flask 类
 def wsgi_app(self, environ, start_response):

        ctx = self.request_context(environ)
        ctx.push()

        error = None
        try:
            try:
                # 4 执行视图函数
                response = self.full_dispatch_request()
            except Exception as e:
                # 异常处理试图报错,包含信号2345报错执行,got_request_exception信号
                error = e
                response = self.handle_exception(e)
            except:
                error = sys.exc_info()[1]
                raise
            # 将处理的内容,返回给用户浏览器
            return response(environ, start_response)
        finally:
            if self.should_ignore_error(error):
                error = None

            # 9、结束
            ctx.auto_pop(error)
self.request_context(environ)
# Flask类
def request_context(self, environ):
       return RequestContext(self, environ)
RequestContext(self, environ)
# RequestContest类
 def __init__(self, app, environ, request=None):
        self.app = app
     # 初始化request请求对象
if request is None: request = app.request_class(environ) self.request = request self.url_adapter = app.create_url_adapter(self.request) self.flashes = None self.session = None

2.通过ctx.puth()建立2个请求堆栈(采用threading.local)

# RequestContext类
def push(self):
        top = _request_ctx_stack.top
        if top is not None and top.preserved:
            top.pop(top._preserved_exc)

        # Before we push the request context we have to ensure that there
        # is an application context.
        app_ctx = _app_ctx_stack.top
        if app_ctx is None or app_ctx.app != self.app:
            # app_ctx = AppContext(self.app) --> _implictit_app_ctx_stack(app,g)
            app_ctx = self.app.app_context()
            app_ctx.push()
            self._implicit_app_ctx_stack.append(app_ctx)
        else:
            self._implicit_app_ctx_stack.append(None)

        if hasattr(sys, exc_clear):
            sys.exc_clear()

        ‘‘‘
        请求相关数据,加到local中
        ‘‘‘
        _request_ctx_stack.push(self)


        # 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()

3._implicit_app_ctx_stack 调用

--app_ctx(app,g) --> self._implicit_app_ctx_stack.append(app_ctx)堆栈

top = _request_ctx_stack.top

_request_ctx_stack = LocalStack()
class LocalStack(object):

    def __init__(self):
        self._local = Local()

    def __release_local__(self):
        self._local.__release_local__()

    def _get__ident_func__(self):
        return self._local.__ident_func__

    def _set__ident_func__(self, value):
        object.__setattr__(self._local, ‘__ident_func__‘, value)

    __ident_func__ = property(_get__ident_func__, _set__ident_func__)
    del _get__ident_func__, _set__ident_func__

    def __call__(self):
        def _lookup():
            rv = self.top
            if rv is None:
                raise RuntimeError(‘object unbound‘)
            return rv
        return LocalProxy(_lookup)

    def push(self, obj):
        """Pushes a new item to the stack"""
        rv = getattr(self._local, ‘stack‘, None)
        if rv is None:
            self._local.stack = rv = []
        rv.append(obj)
        return rv

    def pop(self):
        """Removes the topmost item from the stack, will return the
        old value or `None` if the stack was already empty.
        """
        stack = getattr(self._local, ‘stack‘, None)
        if stack is None:
            return None
        elif len(stack) == 1:
            release_local(self._local)
            return stack[-1]
        else:
            return stack.pop()

    @property
    def top(self):
        """The topmost item on the stack.  If the stack is empty,
        `None` is returned.
        """
        try:
            return self._local.stack[-1]
        except (AttributeError, IndexError):
            return None

app_ctx = self.app.app_context()

# Flask类
def app_context(self):
    return AppContext(self)

AppContext(self)

# AppContext类,初始化
def __init__(self, app):
        self.app = app  # 等于Flask对象
        self.url_adapter = app.create_url_adapter(None)
        self.g = app.app_ctx_globals_class() # Flask系统全局变量 

        # Like request context, app contexts can be pushed multiple times
        # but there a basic "refcount" is enough to track them.
        self._refcnt = 0

4._request_ctx_stack调用

--_request_ctx_stack.push(self) (请求相关)(堆栈)

    def push(self, obj):
        """Pushes a new item to the stack"""
        rv = getattr(self._local, stack, None)
        if rv is None:
            self._local.stack = rv = []
        rv.append(obj)
        return rv
self._local
class Local(object):
    __slots__ = (__storage__, __ident_func__)

    def __init__(self):
        object.__setattr__(self, __storage__, {})
        object.__setattr__(self, __ident_func__, get_ident)

    def __iter__(self):
        return iter(self.__storage__.items())

    def __call__(self, proxy):
        """Create a proxy for a name."""
        return LocalProxy(self, proxy)

    def __release_local__(self):
        self.__storage__.pop(self.__ident_func__(), None)

    def __getattr__(self, name):
        try:
            return self.__storage__[self.__ident_func__()][name]
        except KeyError:
            raise AttributeError(name)

    def __setattr__(self, name, value):
        ident = self.__ident_func__()
        storage = self.__storage__
        try:
            storage[ident][name] = value
        except KeyError:
            storage[ident] = {name: value}

    def __delattr__(self, name):
        try:
            del self.__storage__[self.__ident_func__()][name]
        except KeyError:
            raise AttributeError(name)

 3、从栈中将请求对象调取出来

1.经过层层建立到达视图函数

full_dispatch_request-->self.dispatch_request()-->dispatch_request
# Flask类   
def full_dispatch_request(self): """Dispatches the request and on top of that performs request pre and postprocessing as well as HTTP exception catching and error handling. .. versionadded:: 0.7 """ self.try_trigger_before_first_request_functions() try: request_started.send(self) rv = self.preprocess_request() if rv is None: # 触发执行视图函数,使用session rv = self.dispatch_request() except Exception as e: rv = self.handle_user_exception(e) return self.finalize_request(rv)
dispatch_request(self)
def dispatch_request(self):
        req = _request_ctx_stack.top.request
        if req.routing_exception is not None:
            self.raise_routing_exception(req)
        rule = req.url_rule
        # if we provide automatic options for this URL and the
        # request came with the OPTIONS method, reply automatically
        if getattr(rule, provide_automatic_options, False)            and req.method == OPTIONS:
            return self.make_default_options_response()
        # otherwise dispatch to the handler for that endpoint
        return self.view_functions[rule.endpoint](**req.view_args)    
 --request  --->_lookup_req_object

  --session --->_lookup_req_object

  --current_app--->_find_app

  --g--->_lookup_app_object

current_app = LocalProxy(_find_app)
request = LocalProxy(partial(_lookup_req_object, request))
session = LocalProxy(partial(_lookup_req_object, session))
g = LocalProxy(partial(_lookup_app_object, g))
LocalProxy
@implements_bool
class LocalProxy(object):

    __slots__ = (__local, __dict__, __name__, __wrapped__)

    def __init__(self, local, name=None):
        object.__setattr__(self, _LocalProxy__local, local)
        object.__setattr__(self, __name__, name)
        if callable(local) and not hasattr(local, __release_local__):
            # "local" is a callable that is not an instance of Local or
            # LocalManager: mark it as a wrapped function.
            object.__setattr__(self, __wrapped__, local)

    def _get_current_object(self):
        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__)

2.以上四个对象通过localProxy(采用threading.local),调用

  --_lookup_app_object

def _lookup_req_object(name):
    top = _request_ctx_stack.top
    if top is None:
        raise RuntimeError(_request_ctx_err_msg)
    return getattr(top, name)

  --_find_app

def _find_app():
    top = _app_ctx_stack.top
    if top is None:
        raise RuntimeError(_app_ctx_err_msg)
    return top.app

  --_lookup_app

def _lookup_app_object(name):
    top = _app_ctx_stack.top
    if top is None:
        raise RuntimeError(_app_ctx_err_msg)
    return getattr(top, name)

3.以上三个方法调用

  _lookup_req_object ----> _implicit_app_ctx_stack

  _find_app和_lookup_app  ----> _request_ctx_stack

Flask系列之源码分析(二)

标签:blog   nal   pat   current   read   got   ali   hat   object   

原文地址:https://www.cnblogs.com/wangshuyang/p/8954331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!