作家
登录

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

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

  •     # get rid of circular dependencies at the end of the request 
  •  
  •     # so that we don't require the GC to be active. 
  •  
  •     if clear_request: 
  •  
  •         rv.request.environ['werkzeug.request'] = None 
  •  
  •   
  •  
  •     # Get rid of the app as well if necessary. 
  •  
  •     if app_ctx is not None: 
  •  
  •         app_ctx.pop(exc)  
  • 膳绫擎代码中的细节先不评论辩论。留意到当要分开以上“请求高低文”情况的时刻,Flask会先将“请求高低文”对象大年夜_request_ctx_stack栈中烧毁,之后会根据实际的情况肯定烧毁“应用高低文”对象。

    1. # example - Flask v0.9 
    2.  
    3. >>> from flask import Flask, _request_ctx_stack, _app_ctx_stack 
    4.  
    5. # 创建两个Flask应用 
    6.  
    7. >>> app = Flask(__name__) 
    8.  
    9. >>> app2 = Flask(__name__) 
    10.  
    11. # 先查看两个栈中的内容 
    12.  
    13. >>> _request_ctx_stack._local.__storage__ 
    14.  
    15. {} 
    16.  
    17. >>> _app_ctx_stack._local.__storage__ 
    18.  
    19. {} 
    20.  
    21. # 构建一个app的请求高低文情况,在这个情况中运行app2的相干操作 
    22.  
    23. >>> with app.test_request_context(): 
    24.  
    25.         print "Enter app's Request Context:" 
    26.  
    27.         print _request_ctx_stack._local.__storage__ 
    28.  
    29.         print _app_ctx_stack._local.__storage__ 
    30.  
    31.         print 
    32.  
    33.         with app2.app_context(): 
    34.  
    35.             print "Enter app2's App Context:" 
    36.  
    37.             print _request_ctx_stack._local.__storage__ 
    38.  
    39.             print _app_ctx_stack._local.__storage__ 
    40.  
    41.             print 
    42.  

        推荐阅读

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

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


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

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

    关键词: 探索发现

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

    网友点评
    自媒体专栏

    评论

    热度

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