django开发者模式中的autoreload是怎样实现的

    在开发django应用的过程中,使用开发者模式启动服务是特别方便的一件事,只需要 python manage.py runserver 就可以运行服务,并且提供了非常人性化的autoreload机制,不需要手动重启程序就可以修改代码并看到反馈。刚接触的时候觉得这个功能比较人性化,也没觉得是什么特别高大上的技术。后来有空就想着如果是我来实现这个autoreload会怎么做,想了很久没想明白,总有些地方理不清楚,看来第一反应真是眼高手低了。于是就专门花了一些时间研究了django是怎样实现autoreload的,每一步都看源码说话,不允许有丝毫的想当然:

1、runserver命令。在进入正题之前其实有一大段废话,是关于runserver命令如何执行的,和主题关系不大,就简单带一下:
命令行键入 python manage.py runserver 后,django会去寻找runserver这个命令的执行模块,最后落在
django\contrib\staticfiles\management\commands\runserver.py模块上:

#django\contrib\staticfiles\management\commands\runserver.py
from django.core.management.commands.runserver import \
Command as RunserverCommandclass Command(RunserverCommand):help = "Starts a lightweight Web server for development and also serves static files."

而这个Command的执行函数在这:

#django\core\management\commands\runserver.py
class Command(BaseCommand):def run(self, **options):"""Runs the server, using the autoreloader if needed"""use_reloader = options['use_reloader']if use_reloader:autoreload.main(self.inner_run, None, options)else:self.inner_run(None, **options)

这里有关于use_reloader的判断。如果我们在启动命令中没有加--noreload,程序就会走autoreload.main这个函数,如果加了,就会走self.inner_run,直接启动应用。
其实从autoreload.main的参数也可以看出,它应该是对self.inner_run做了一些封装,autoreload的机制就在这些封装当中,下面我们继续跟。

PS: 看源码的时候发现django的command模式还是实现的很漂亮的,值得学习。

2、autoreload模块。看autoreload.main():

#django\utils\autoreload.py:
def main(main_func, args=None, kwargs=None):if args is None:args = ()if kwargs is None:kwargs = {}if sys.platform.startswith('java'):reloader = jython_reloaderelse:reloader = python_reloaderwrapped_main_func = check_errors(main_func)reloader(wrapped_main_func, args, kwargs)

这里针对jpython和其他python做了区别处理,先忽略jpython;check_errors就是把对main_func进行错误处理,也先忽略。看python_reloader:

#django\utils\autoreload.py:
def python_reloader(main_func, args, kwargs):if os.environ.get("RUN_MAIN") == "true":thread.start_new_thread(main_func, args, kwargs)try:reloader_thread()except KeyboardInterrupt:passelse:try:exit_code = restart_with_reloader()if exit_code < 0:os.kill(os.getpid(), -exit_code)else:sys.exit(exit_code)except KeyboardInterrupt:pass

第一次走到这里时候,环境变量中RUN_MAIN变量不是"true", 甚至都没有,所以走else, 看restart_with_reloader:

#django\utils\autoreload.py:
def restart_with_reloader():while True:args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argvif sys.platform == "win32":args = ['"%s"' % arg for arg in args]new_environ = os.environ.copy()new_environ["RUN_MAIN"] = 'true'exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)if exit_code != 3:return exit_code    

这里首先起一个while循环, 内部先把RUN_MAIN改成了"true",然后用os.spawnve方法开一个子进程(subprocess),看看os.spawnve的说明:

#os.py
def
spawnve(mode, file, args, env):"""spawnve(mode, file, args, env) -> integerExecute file with arguments from args in a subprocess with thespecified environment.If mode == P_NOWAIT return the pid of the process.If mode == P_WAIT return the process's exit code if it exits normally;otherwise return -SIG, where SIG is the signal that killed it. """
return _spawnvef(mode, file, args, env, execve)

其实就是再调一遍命令行,又走了一遍 python manage.py runserver。

接着看restart_with_reloader里的while循环,需要注意的是while循环退出的唯一条件是exit_code!=3。 如果子进程不退出,就一直停在 os.spawnve这一步; 如果子进程退出,而退出码不是3,while就被终结了;如果是3,继续循环,重新创建子进程。从这个逻辑可以猜想autoreload的机制:当前进程(主进程)其实啥也不干,就监视子进程的运行状况,子进程才是真正干事儿的;如果子进程以exit_code=3退出(应该由于检测到了文件修改),就再启动一遍子进程,新代码自然就生效了;如果子进程以exit_code!=3退出,主进程也结束,整个django程序就算跪了。这只是猜想,下面接着来验证。

3、子进程。上面其实有一个疑问,既然是重新启动了一次,为什么子进程不会接着生成子进程?原因就在于RUN_MAIN这个环境变量,主进程中把它改成了true,子进程走到python_reloader函数的时候:

#django\utils\autoreload.py:
def python_reloader(main_func, args, kwargs):if os.environ.get("RUN_MAIN") == "true":thread.start_new_thread(main_func, args, kwargs)try:reloader_thread()except KeyboardInterrupt:passelse:try:exit_code = restart_with_reloader()if exit_code < 0:os.kill(os.getpid(), -exit_code)else:sys.exit(exit_code)except KeyboardInterrupt:pass

if条件满足了,和主进程走了不一样的逻辑分支。在这里,首先去开一个线程,运行main_func,就是上文的 Command.inner_run。这里的thread模块是这么import的:

#django\utils\autoreload.py:
from django.utils.six.moves import _thread as thread

这里six模块的作用是兼容各种python版本:

[codeblock six]
#django\utils\six.py
class _SixMetaPathImporter(object):"""
A meta path importer to import six.moves and its submodules.This class implements a PEP302 finder and loader. It should be compatible
with Python 2.5 and all existing versions of Python3
"""官网说明:
# https://pythonhosted.org/six/
Six: Python 2 and 3 Compatibility Library
Six provides simple utilities for wrapping over differences between Python 2 and Python 3. It is intended to support codebases that work on both Python 2 and 3 without modification. six consists of only one Python file, so it is painless to copy into a project.

所以如果程序想在python2和python3上都能跑,且鲁邦,six是重要的工具。之后抽个时间看下six,mark一下。

然后再开一个reloader_thread:

[codeblock autoreload_reloader_thread]
#django\utils\autoreload.py:
def reloader_thread():ensure_echo_on()if USE_INOTIFY:fn = inotify_code_changedelse:fn = code_changed
while RUN_RELOADER:change = fn()if change == FILE_MODIFIED:sys.exit(3) # force reloadelif change == I18N_MODIFIED:reset_translations()time.sleep(1)

ensure_echo_on()其实还没看明白,貌似是针对类unix系统文件处理的,先略过;
USE_INOTIFY也是系统文件操作相关的变量,根据 inotify 是否可用选择检测文件变化的方法。
while循环,每隔1秒检测一下文件状态,如果是普通文件有变化,进程退出,退出码为3,主进程一看:退出码是3,就重启子进程。。。。这样就和上面连上了;如果不是普通文件变化,而是I18N_MODIFIED(.mo后缀的文件变化,二进制库文件之类的),那就 reset_translations ,大概意思是把已加载过的库缓存清理掉,下次重新加载。

  以上就是autoreload机制的流程。其中还是有些细节不是特别清楚,比如不同操作系统文件变化的检测,但都是很细节的东西了,不涉及主流程。看完这些,我又问了自己一遍,如果是让我设计autoreload机制会怎样搞。现在我的答案是:直接把 django\utils\autoreload.py 文件拿来用啊。其实这是很独立的一个模块,而且特别通用,完全可以作为通用的autoreload解决方案,我还自己写个毛啊。

转载于:https://www.cnblogs.com/beeler/p/7382917.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369640.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html5与css3入门知识点精炼

<meta name "keywords" content"…………"/>&#xff08;网页搜索时要输入的关键字&#xff09;<meta name "author" content "作者的名字"<meta http-equiv "refresh" content "跳转的时间 ; URL跳转…

CSS实现单行、多行文本溢出显示省略号(…)

如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来&#xff0c;当然还需要加宽度width属来兼容部分浏览。 实现方法&#xff1a; overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 效果如图&#xff1a; 但是这个属性只支持单行…

java的方法是什么用,Java中的本机方法是什么?它们应该在何处使用?

A native method has the same syntax as an abstract method, but where is it implemented?解决方案What are native methods in Java and where should they be used?Once you see a small example, it becomes clear:Main.java:public class Main {public native int int…

JAXB –表示空集合和空集合

示范代码 以下演示代码将用于Java模型的所有不同版本。 它只是将一个集合设置为null&#xff0c;第二个设置为空列表&#xff0c;第三个设置为填充列表。 package package blog.xmlelementwrapper;import java.util.ArrayList; import javax.xml.bind.*;public class Demo {pu…

显示日历的指令:cal

1.显示日历的指令&#xff1a;cal &#xff08;1&#xff09;参数&#xff1a; &#xff08;2&#xff09;实例&#xff1a; 转载于:https://www.cnblogs.com/yfacesclub/p/8434449.html

简单好用的计算器:bc

1.简单好用的计算器&#xff1a;bc &#xff08;1&#xff09;参数&#xff1a; &#xff08;2&#xff09;实例&#xff1a; 执行浮点运算和一些高级函数 设定小数精度&#xff08;数值范围&#xff09; 进制转换 执行结果为&#xff1a;11000000&#xff0c;这是用bc将十进制…

Day2 第一次写python

写代码只要会Cpython就可以了Java虚拟机即可执行python代码对于Java代码 也会生成中间代码 做成虚拟机 pypy python代码 先变成字节码 再变成机器码 计算机即可识别 pypy&#xff1a;直接把代码转换成机器码 2.7 可以不加加括号3.6 一定要写括号 #&#xff01;/user/bin/python…

java注解类型命名_第三十九条:注解优先于命名模式

根据经验&#xff0c;一般使用命令模式表明有些程序元素需要通过某种工具或者框架进行特殊处理。例如&#xff0c;在Java4发行版本之前&#xff0c;JUnit测试框架原本要求用户一定要用test作为测试方法名称的开头。这种方法可行&#xff0c;但是有几个很严重的缺点。首先&#…

查看Servlet 3.0的新增功能

随着JEE6规范上市&#xff0c;在企业应用程序领域中如何开发应用程序方面发生了一些重大变化。 在本文中&#xff0c;我将介绍有关Web应用程序开发的一些更改。 首先&#xff0c;请告别web.xml部署描述符&#xff08;至少是其中的一部分&#xff09;。 好吧&#xff0c;它并不是…

block,inline,inline-block的区别

最近正在复习&#xff0c;紧张地准备几天后的笔试&#xff0c;然后刚好看到这个地方。 block&#xff1a;块级元素&#xff0c;会换行&#xff0c;如div,p,h1~h6,table这些&#xff0c;可以设置宽高&#xff1b; inline:行内元素&#xff0c;不换行&#xff0c;挤在一行显示&am…

假期(模块相关)

# ---------------------------------------------------------------------------------------- import time timestamp time.time() #时间戳 struct_time time.localtime() #结构化时间 format_time time.strftime("%Y-%m-%d %X") #格式化时间# print…

anyproxy抓取移动http、https请求

windows下安装AnyProxy抓取移动App Http请求AnyProxy是阿里巴巴基于 Node.js 开发的一款开源代理服务器。做为中间代理服务器&#xff0c;它可以收集所有经过它的http请求流量&#xff08;包括https明文内容&#xff09;&#xff1b;它提供了友好的web界面&#xff0c;便于直观…

振作起来– Spring Framework 4.0即将来临!

几天前&#xff0c;SpringSource 宣布流行的Spring框架的4.0版本正在开发中。 下一个迭代将是Spring Framework 4.0&#xff01; 如SpringSource所言&#xff0c;即将发布的版本的重点是“ 2013年及以后出现的企业主题”&#xff1a; 支持Java SE 8 Spring应用程序 使用Groo…

java内存管理课程设计_Java内存管理分析

Java内存主要分为stack, heap, data segment, and code segment.stack(栈)&#xff1a;存放非静态基本数据类型变量的名称和值&#xff0c;以及非静态对象的引用若是非静态基本数据类型变量&#xff0c;则变量的名称和值一起被存入stack(栈)中&#xff0c;变量的名称指向变量的…

Windows 10 IoT Core 17101 for Insider 版本更新

除夕夜&#xff0c;微软发布了Windows 10 IoT Core 17101 for Insider 版本更新&#xff0c;本次更新只修正了一些Bug&#xff0c;没有发布新的特性。已知的问题: F5 driver deployment from Visual Studio does not work on IoT Core.F5 application deployment of headed f…

Spring Batch中的块处理

大数据集的处理是软件世界中最重要的问题之一。 Spring Batch是一个轻量级且强大的批处理框架&#xff0c;用于处理数据集。 Spring Batch Framework提供了“面向TaskletStep”和“面向块”的处理风格。 在本文中&#xff0c;将解释面向块的处理模型。 此外&#xff0c;绝对建…

type=file文件上传H5新特性

1、语法 <input name"myFile" type"file"> 2、属性&#xff08;以下三个仅 HTML5支持&#xff0c;因此存在兼容性问题&#xff09;&#xff08;1&#xff09;multiple &#xff1a;表示用户是否可以选择多个值。multiple只能用于typefile和typeemail…

关于git的一些文章

为什么Github没有记录你的Contributions转载于:https://www.cnblogs.com/xiaobie123/p/7391266.html

epoll学习

一、epoll_create #include <sys/epoll.h>int epoll_create(int size); int epoll_create1(int flags); 返回&#xff1a;成功非负文件描述符&#xff0c;-1出错size:内核监听数目一共多大 创建一个epoll接口&#xff0c;size参数和select不同&#xff0c;不是fd1&#x…