国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

淺談django開發者模式中的autoreload是如何實現的

2019-11-25 15:55:36
字體:
來源:轉載
供稿:網友

在開發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.pyfrom 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.pyclass 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模塊??碼utoreload.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_reloader  else:    reloader = python_reloader  wrapped_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:      pass  else:    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.argv    if 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.pydef spawnve(mode, file, args, env):  """spawnve(mode, file, args, env) -> integer  Execute file with arguments from args in a subprocess with the  specified 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:      pass  else:    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.pyclass _SixMetaPathImporter(object):"""A meta path importer to import six.moves and its submodules.This class implements a PEP302 finder and loader. It should be compatiblewith Python 2.5 and all existing versions of Python3"""官網說明:# https://pythonhosted.org/six/Six: Python 2 and 3 Compatibility LibrarySix 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_changed  else:    fn = code_changed  while RUN_RELOADER:    change = fn()    if change == FILE_MODIFIED:      sys.exit(3) # force reload    elif 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機制會怎樣搞?,F在我的答案是:直接把 django/utils/autoreload.py 文件拿來用啊。其實這是很獨立的一個模塊,而且特別通用,完全可以作為通用的autoreload解決方案,我還自己寫個毛啊。

這篇淺談django開發者模式中的autoreload是如何實現的就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 斗六市| 同德县| 内江市| 鸡东县| 噶尔县| 五家渠市| 镇安县| 枣阳市| 吉木萨尔县| 本溪市| 江华| 文化| 怀柔区| 武定县| 丹江口市| 蓝田县| 长顺县| 新宁县| 舞阳县| 文成县| 阿勒泰市| 温州市| 松潘县| 清徐县| 海门市| 宁阳县| 武宣县| 开鲁县| 德惠市| 庆元县| 福建省| 新余市| 多伦县| 太保市| 靖江市| 拉萨市| 大埔县| 离岛区| 盈江县| 微山县| 敖汉旗|