理解Django 中Call Stack机制的小Demo


Posted in Python onSeptember 01, 2020

1.工作流程

request/response模式下,request并不是直接到达view方法,view方法也不是将返回的response直接发送给浏览器的,而是request由外到里的层层通过各种middleware层,这个时候可以对request做一些事情,到最后一层也就是最内层时,得到view方法返回的response,然后再把这个response再由内到外层层传递出来,这时候可以对response做一些事情,如下图:

理解Django 中Call Stack机制的小Demo

2.原理

class SimpleMiddleware:
 def __init__(self, get_response):
  self.get_response = get_response
  # One-time configuration and initialization.
 
 def __call__(self, request):
  # Code to be executed for each request before
  # the view (and later middleware) are called.
 
  response = self.get_response(request)
 
  # Code to be executed for each request/response after
  # the view is called.
 
  return response

每个middleware都如上面代码一样,有两个必须的函数:(1)__init__(self,get_response),负责在web server启动时完成初始化,即建立与下一层middleware的联系.(2)__call__(self,request),在每次request下,被调用。

有三个可选的函数:(1)process_view:__init__和__call__对view 方法一无所知,而process_view给了通道(give access to)在调用view方法之前获晓view方法及其参数,如果出现了,则它在__call__调用后,在self.get_response(request)之前调用,因此可以触发view 方法。(2)process_exception:如果在view方法中出现异常,该函数可以提供机会来处理异常(3)process_template_response:在self.get_response(request)调用后,view方法结束后,提供修改response的机会,需要注意的是该函数仅仅在view方法返回的是TemplateResponse类的情况下有效,如果返回的是render() ,则该函数不被触发。

详见 https://docs.djangoproject.com/en/3.1/topics/http/middleware/

(3)供进一步理解call stack 机制的小demo(仅供参考)

import time
class Base:
 def __init__(self,get_response):
  self.get_response=get_response
 def __call__(self,request=None):
  self.pre()
  response=self.get_response(request)
  self.after()
  return response
 def pre(self):
  pass
 def after(self):
  pass
class Response:
 def __init__(self):
  pass
def view(request):
 return Response()
def startServer(callstackSets,view):
 callstackSets.reverse()
 last=callstackSets.pop(0)(view)
 for i in range(len(callstackSets)):
  last=callstackSets.pop(0)(last)
 return last
class A(Base):
 def pre(self):
  print('In A')
 def after(self):
  print('Out A ')
class B(Base):
 def pre(self):
  print('In B')
 def after(self):
  print('Out B ')
class C(Base):
 def pre(self):
  print('In C')
 def after(self):
  print('Out C ')
if __name__=='__main__':
 callstackSets=[A,B,C]
 calls=startServer(callstackSets,view)
 calls('req')

运行后:

理解Django 中Call Stack机制的小Demo

可以看到完成了既定的目标,request进来后,一层一层的进入,直到最内层C,调用view方法,得到response,然后将response从最内层一层一层的传递出来!

到此这篇关于理解Django 中Call Stack机制的小Demo的文章就介绍到这了,更多相关Django Call Stack机制内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
一则python3的简单爬虫代码
May 26 Python
python检测远程端口是否打开的方法
Mar 14 Python
python让图片按照exif信息里的创建时间进行排序的方法
Mar 16 Python
Python实现更改图片尺寸大小的方法(基于Pillow包)
Sep 19 Python
Python多进程库multiprocessing中进程池Pool类的使用详解
Nov 24 Python
Python中list查询及所需时间计算操作示例
Jun 21 Python
Python使用gRPC传输协议教程
Oct 16 Python
解决PyCharm控制台输出乱码的问题
Jan 16 Python
Python通过TensorFlow卷积神经网络实现猫狗识别
Mar 14 Python
Python实用库 PrettyTable 学习笔记
Aug 06 Python
解决Djang2.0.1中的reverse导入失败的问题
Aug 16 Python
浅谈numpy中函数resize与reshape,ravel与flatten的区别
Jun 18 Python
如何快速理解python的垃圾回收机制
Sep 01 #Python
Python Opencv图像处理基本操作代码详解
Aug 31 #Python
Python Matplotlib绘图基础知识代码解析
Aug 31 #Python
一些关于python 装饰器的个人理解
Aug 31 #Python
Python常用模块函数代码汇总解析
Aug 31 #Python
PyTorch 导数应用的使用教程
Aug 31 #Python
PyTorch安装与基本使用详解
Aug 31 #Python
You might like
简单采集了yahoo的一些数据
2007/02/14 PHP
php 破解防盗链图片函数
2008/12/09 PHP
服务器web工具 php环境下
2010/12/29 PHP
浅析PHP页面局部刷新功能的实现小结
2013/06/21 PHP
PHP修改session_id示例代码
2014/01/08 PHP
php无限遍历目录示例
2014/02/21 PHP
destoon供应信息title调用出公司名称的方法
2014/08/22 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
2016/03/21 PHP
PHP使用pdo连接access数据库并循环显示数据操作示例
2018/06/05 PHP
JQuery从头学起第三讲
2010/07/06 Javascript
javascript面向对象特性代码实例
2014/06/12 Javascript
json传值以及ajax接收详解
2016/05/24 Javascript
JavaScript动态检验密码强度的实现方法
2016/11/09 Javascript
JS实现图片放大缩小的方法
2017/02/15 Javascript
JavaScript Drum Kit 指南(纯 JS 模拟敲鼓效果)
2017/07/23 Javascript
NodeJS安装图文教程
2018/04/19 NodeJs
JS利用prototype给类添加方法操作详解
2019/06/21 Javascript
JavaScript实现电灯开关小案例
2020/03/30 Javascript
[02:46]2014DOTA2国际邀请赛 选手为你解读比赛MVP充满梦想
2014/07/09 DOTA
[01:03:13]VG vs Pain 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python实现简单的计时器功能函数
2015/03/14 Python
Python中的自省(反射)详解
2015/06/02 Python
Python xlwt设置excel单元格字体及格式
2020/04/18 Python
python 信息同时输出到控制台与文件的实例讲解
2018/05/11 Python
python 使用正则表达式按照多个空格分割字符的实例
2018/12/20 Python
基于python实现查询ip地址来源
2020/06/02 Python
Python制作一个仿QQ办公版的图形登录界面
2020/09/22 Python
Python plt 利用subplot 实现在一张画布同时画多张图
2021/02/26 Python
CSS3样式linear-gradient的使用实例
2017/01/16 HTML / CSS
Toppik顶丰增发纤维官网:解决头发稀疏
2017/12/30 全球购物
伦敦鲜花递送:Flower Station
2021/02/03 全球购物
华为c/c++笔试题
2016/01/25 面试题
简述网络文件系统NFS,并说明其作用
2016/10/19 面试题
文明教师事迹材料
2014/01/16 职场文书
公司任命书范本
2014/06/04 职场文书
使用MybatisPlus打印sql语句
2022/04/22 SQL Server