理解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 相关文章推荐
Python使用MYSQLDB实现从数据库中导出XML文件的方法
May 11 Python
Python Requests 基础入门
Apr 07 Python
Python进程间通信Queue实例解析
Jan 25 Python
使用pandas批量处理矢量化字符串的实例讲解
Jul 10 Python
浅谈tensorflow中几个随机函数的用法
Jul 27 Python
python3.4控制用户输入与输出的方法
Oct 17 Python
对python函数签名的方法详解
Jan 22 Python
python 获得任意路径下的文件及其根目录的方法
Feb 16 Python
Python列表常见操作详解(获取,增加,删除,修改,排序等)
Feb 18 Python
Python实现投影法分割图像示例(二)
Jan 17 Python
使用python matplotlib 画图导入到word中如何保证分辨率
Apr 16 Python
Python数据结构之队列详解
Mar 21 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
PHP获取当前页面完整URL的实现代码
2013/06/10 PHP
yii2.0整合阿里云oss的示例代码
2017/09/19 PHP
php字符串函数 str类常见用法示例
2020/05/15 PHP
JQuery 获取和设置Select选项的代码
2010/02/07 Javascript
纯js分页代码(简洁实用)
2013/11/05 Javascript
浅析IE10兼容性问题(frameset的cols属性)
2014/01/03 Javascript
7个JS基础知识总结
2014/03/05 Javascript
node.js中使用q.js实现api的promise化
2014/09/17 Javascript
jQuery基于ajax实现带动画效果无刷新柱状图投票代码
2015/08/10 Javascript
JS实现漂亮的淡蓝色滑动门效果代码
2015/09/23 Javascript
通过修改360抢票的刷新频率和突破8车次限制实现方法
2017/01/04 Javascript
Vue.js划分组件的方法
2017/10/29 Javascript
详解redis在nodejs中的应用
2018/05/02 NodeJs
ES6之模版字符串的具体使用
2018/05/17 Javascript
js实现移动端轮播图
2020/12/21 Javascript
layui 解决富文本框form表单提交为空的问题
2019/10/26 Javascript
如何优雅地取消 JavaScript 异步任务
2020/03/22 Javascript
Python实现将SQLite中的数据直接输出为CVS的方法示例
2017/07/13 Python
Python实现的求解最小公倍数算法示例
2018/05/03 Python
Python UnboundLocalError和NameError错误根源案例解析
2018/10/31 Python
Python字符串中添加、插入特定字符的方法
2019/09/10 Python
Python读写csv文件流程及异常解决
2020/10/20 Python
Python提取视频中图片的示例(按帧、按秒)
2020/10/22 Python
英国游戏机和游戏购物网站:365games.co.uk
2018/06/18 全球购物
什么是规则表达式
2012/05/03 面试题
我爱我校演讲稿
2014/05/21 职场文书
中国文明网向国旗敬礼寄语大全
2014/09/27 职场文书
机票销售员态度不好检讨书
2014/09/27 职场文书
离婚协议书样本
2015/01/26 职场文书
祝寿主持词
2015/07/02 职场文书
2016入党心得体会范文
2016/01/06 职场文书
高三化学教学反思
2016/02/22 职场文书
创业计划书之养殖业
2019/10/11 职场文书
python中opencv实现图片文本倾斜校正
2021/06/11 Python
JS数组方法some、every和find的使用详情
2021/10/05 Javascript
Vue的过滤器你真了解吗
2022/02/24 Vue.js