Flask 的整体流程

封装 requestContext 对象, full_dispatch_request(视图函数 执行), response返回

从app.run() 开始 -->>

Flask的call方法-->>

wsgi_app (封装RequestContext(request,session )对象到 localstack) -->>

full_dispatch_request(视图函数 执行) -->>

执行扩展(before_request) ,触发信号 -->>

获取response -->>

pop reqeust,session -- >>

结束

(1)threading local 和 flask的自定义local对象 

		- 基于本地线程 可以实现,为了支持协程,自己定义了一个local对象

(2) 请求到来
封装 ctx = RequestContext(request,session)
ctx -- 放入 Local __storage__ { 'id':{stack:[ctx]} } (3)执行视图
导入 request print(reqeust) -- >> localproxy __str__
reqeust.method -- >> localproxy __getattr__
reqeust + 1 -- >> localproxy __add__ 调用 _lookup_req_object 函数 去local中的ctx 中 获取 reqeust session (4) 请求结束
ctx.auto_pop
ctx 从 local 中移除

class RequestContext(object):
def push(self):
_request_ctx_stack.push(self) # 将请求的相关数据 加到 local中,stack,local self.session = self.app.open_session(self.request)
if self.session is None:
self.session = self.app.make_null_session()
# 将请求的相关数据 加到 local中,stack,local
# 获取cookie中的随机字符串,检验是否存在,没有就生成一个
# 根据随机字符串,获取服务端保存的 session的值 class Flask(_PackageBoundObject): def __call__(self, environ, start_response):
return self.wsgi_app(environ, start_response) def wsgi_app(self, environ, start_response):
ctx = self.request_context(environ) # 创建request对象
ctx.push() # 处理 request 和 session
error = None
try:
try:
response = self.full_dispatch_request() #视图函数 执行
return response(environ, start_response)
finally:
ctx.auto_pop(error) def full_dispatch_request(self):
"""
self.try_trigger_before_first_request_functions()
try:
request_started.send(self) # 触发信号,需要下载 blinker
rv = self.preprocess_request()
if rv is None:
rv = self.dispatch_request()
return self.finalize_request(rv) # 获取response 封装 def finalize_request(self, rv, from_error_handler=False): response = self.make_response(rv)
try:
response = self.process_response(response)
return response def process_response(self, response):
ctx = _request_ctx_stack.top
bp = ctx.request.blueprint
funcs = ctx._after_request_functions
if not self.session_interface.is_null_session(ctx.session):
self.save_session(ctx.session, response)
return response

Flask 的整体流程的更多相关文章

  1. 使用git整体流程

    一.git提交代码走meger请求的整体流程 工作中使用git推代码时,如果走merge请求,那么也就是说拉代码时拉公共代码库的代码,但是提交时需要先提交到自己的代码库,然后在gitlab上提交mer ...

  2. Mybatis技术原理理——整体流程理解

    前言:2018年,是最杂乱的一年!所以你看我的博客,是不是很空! 网上有很多关于Mybatis原理介绍的博文,这里介绍两篇我个人很推荐的博文 Mybatis3.4.x技术内幕和 MyBaits源码分析 ...

  3. iOS开发从申请开发账号到APP上架的整体流程详解

    应公司要求,写一份文档从申请账号一直到APP上架的整体流程,下面进入正文. https://blog.csdn.net/qq_35612929/article/details/78754470 首先第 ...

  4. enzyme design 整体流程及感想

    想起什么来写什么吧. 整体流程(以Ceas2, TPP, G3P为例): 准备蛋白即配体参数文件: 设置CST文件: 准备protocol和flag文件: 运行enzyme_design: 结果处理. ...

  5. 【驱动】input子系统整体流程全面分析(触摸屏驱动为例)【转】

    转自:http://www.cnblogs.com/lcw/p/3294356.html input输入子系统整体流程 input子系统在内核中的实现,包括输入子系统(Input Core),事件处理 ...

  6. vue框架整体流程

    1.整体流程 (1)模板解析成render函数 (2)响应式监听 (3)首次渲染,显示页面,绑定依赖 (4)data属性变化,触发rerender 2.模板解析为render函数 参考上一篇博客. 模 ...

  7. linux input输入子系统分析《四》:input子系统整体流程全面分析

    1      input输入子系统整体流程 本节分析input子系统在内核中的实现,包括输入子系统(Input Core),事件处理层(Event Handler)和设备驱动层.由于上节代码讲解了设备 ...

  8. 微信APP支付整体流程记录备忘

      支付整体流程见文档:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_3   商户系统和微信支付系统主要交互说明:     步骤1: ...

  9. (转)linux内存源码分析 - 内存回收(整体流程)

    http://www.cnblogs.com/tolimit/p/5435068.html------------linux内存源码分析 - 内存回收(整体流程) 概述 当linux系统内存压力就大时 ...

随机推荐

  1. CG group

    Linux CGroup全称Linux Control Group, 是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如CPU.内存.磁盘输入输出等).这个项目最早是由Google ...

  2. [Python] Send emails to the recepients specified in Message["CC"]

    Recently, I'm working on a small program which needs to send emails to specific accounts. When I wan ...

  3. C#检测两个文件内容是否相同

    不知道为什么对Excel 2010 xlsx后缀的文件没有效果,求解! 对其他文件有效,如.txt,.csv using System; using System.Security.Cryptogra ...

  4. python并发编程之IO模型(Day38)

    一.IO模型介绍 为了更好的学习IO模型,可以先看同步,异步,阻塞,非阻塞 http://www.cnblogs.com/linhaifeng/articles/7430066.html#_label ...

  5. mybatis入门学习记录(一)

    过硬的技术本领,可以给我们保驾护航,飞得更高.今天开始呢.我们就一起来探讨使用mybatis的好处. 首先我们一起来先看看原生的JDBC对于数据库的操作,然后总结其中的利弊,为学习mybatis奠定基 ...

  6. shiro的Realm

    public class UserRealm extends AuthorizingRealm { private UserService userService = new UserServiceI ...

  7. React-native Android环境搭建

    基础安装 安装Homebrew Homebrew是Mac OSX的包管理器,我们需要通过Homebrew安装开发React Native的相关软件包. 如果不知道怎样安装Homebrew可以点这里:官 ...

  8. 【Flask】WTForms文件上传下载

    # 文件上传笔记:1. 在模版中,form表单中,需要指定`encotype='multipart/form-data'`才能上传文件.2. 在后台如果想要获取上传的文件,那么应该使用`request ...

  9. article标准用法

    article代表一个在文档.页面或者网站中自成一体的内容 其目的是为了让开或重用 譬如论坛的帖子.博客的文章.一片用户的评论.一个互动的widget小工具 article 会有一个标题(通常在hea ...

  10. Spring 是什么