作家
登录

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

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

  •   
  •  
  • # 生成一个请求高低文对象 
  •  
  • >>> request_context = app.test_request_context() 
  •  
  • >>> request_context.push() 
  •  
  •   
  •  
  • # 请求高低文推入栈后,再次查看两个栈的内容 
  •  
  • >>> _request_ctx_stack._local.__storage__ 
  •  
  • {<greenlet.greenlet at 0x6eb32a8>: {'stack': [<RequestContext 'http://localhost/' [GET] of __main__>]}} 
  •  
  • >>> _app_ctx_stack._local.__storage__ 
  •  
  • {<greenlet.greenlet at 0x6eb32a8>: {'stack': [<flask.ctx.AppContext at 0x5c96a58>]}} 
  •  
  •   
  •  
  • >>> request_context.pop() 
  •  
  •   
  •  
  • # 烧毁请求高低文时,再次查看两个栈的内容 
  •  
  • >>> _request_ctx_stack._local.__storage__ 
  •  
  • {} 
  •  
  • >>> _app_ctx_stack._local.__storage__ 
  •  
  • {}  
  • 应用高低文

    上部分中简单介绍了“应用高低文”和“请求高低文”的关系。那什么是“应用高低文”呢?我们先看一下它的类:

    1. class AppContext(object): 
    2.  
    3.     """The application context binds an application object implicitly 
    4.  
    5.     to the current thread or greenlet, similar to how the 
    6.  
    7.     :class:`RequestContext` binds request information.  The application 
    8.  
    9.     context is also implicitly created if a request context is created 
    10.  
    11.     but the application is not on top of the individual application 
    12.  
    13.     context. 
    14.  
    15.     ""
    16.  
    17.   
    18.  
    19.     def __init__(self, app): 
    20.  
    21.         self.app = app 
    22.  
    23.         self.url_adapter = app.create_url_adapter(None) 
    24.  
    25.   

        推荐阅读

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

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


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

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

    关键词: 探索发现

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

    网友点评
    自媒体专栏

    评论

    热度

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