作家
登录

Flask中的请求上下文和应用上下文

作者: 来源: 2017-05-11 16:04:38 阅读 我要评论

  •  
  •   
  •  
  •     _request_ctx_stack.push(self) 
  •  
  •   
  •  
  •     self.session = self.app.open_session(self.request) 
  •  
  •     if self.session is None: 
  •  
  •         self.session = self.app.make_null_session()  
  • 我们留意到,0.9版本的“请求高低文”的pop办法中,当要将一个“请求高低文”推入_request_ctx_stack栈中的时刻,会先检查另一个栈_app_ctx_stack的┞坊顶是否存在“应用高低文”对象或者栈顶的“应用高低文”对象的应用是否是当前应用。如不雅不存在或者不是当前对象,Flask会主动师长教师成一个“应用高低文”对象,并将其推入_app_ctx_stack中。

    我们再看分开高低文时的相干代码:

    1. # Flask v0.9 
    2.  
    3. def pop(self, exc=None): 
    4.  
    5.     """Pops the request context and unbinds it by doing that.  This will 
    6.  
    7.     also trigger the execution of functions registered by the 
    8.  
    9.     :meth:`~flask.Flask.teardown_request` decorator. 
    10.  
    11.   
    12.  
    13.     .. versionchanged:: 0.9 
    14.  
    15.        Added the `exc` argument. 
    16.  
    17.     ""
    18.  
    19.     app_ctx = self._implicit_app_ctx_stack.pop() 
    20.  
    21.   
    22.  
    23.     clear_request = False 
    24.  
    25.     if not self._implicit_app_ctx_stack: 
    26.  
    27.         self.preserved = False 
    28.  
    29.         if exc is None: 
    30.  
    31.             exc = sys.exc_info()[1] 
    32.  
    33.         self.app.do_teardown_request(exc) 
    34.  
    35.         clear_request = True 
    36.  
    37.   
    38.  
    39.     rv = _request_ctx_stack.pop() 
    40.  
    41.     assert rv is self, 'Popped wrong request context.  (%r instead of %r)' 
    42.  
    43.         % (rv, self) 
    44.  
    45.   
    46.  

        推荐阅读

        2025年全球数据将攀升至163ZB,10倍的数据增长来源于哪?

      【51CTO.com原创稿件】比来,希捷科技与IDC合营宣布了一份名为《数据时代2025》的数据成长趋势申报,这份白皮>>>详细阅读


      本文标题:Flask中的请求上下文和应用上下文

      地址:http://www.17bianji.com/lsqh/35165.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)