淺談django開發(fā)者模式中的autoreload是如何實現(xiàn)的
在開發(fā)django應(yīng)用的過程中,使用開發(fā)者模式啟動服務(wù)是特別方便的一件事,只需要 python manage.py runserver 就可以運(yùn)行服務(wù),并且提供了非常人性化的autoreload機(jī)制,不需要手動重啟程序就可以修改代碼并看到反饋。剛接觸的時候覺得這個功能比較人性化,也沒覺得是什么特別高大上的技術(shù)。后來有空就想著如果是我來實現(xiàn)這個autoreload會怎么做,想了很久沒想明白,總有些地方理不清楚,看來第一反應(yīng)真是眼高手低了。于是就專門花了一些時間研究了django是怎樣實現(xiàn)autoreload的,每一步都看源碼說話,不允許有絲毫的想當(dāng)然:
1、runserver命令。在進(jìn)入正題之前其實有一大段廢話,是關(guān)于runserver命令如何執(zhí)行的,和主題關(guān)系不大,就簡單帶一下:
命令行鍵入 python manage.py runserver 后,django會去尋找runserver這個命令的執(zhí)行模塊,最后落在
django\contrib\staticfiles\management\commands\runserver.py模塊上:
#django\contrib\staticfiles\management\commands\runserver.py from django.core.management.commands.runserver import \ Command as RunserverCommand class Command(RunserverCommand): help = "Starts a lightweight Web server for development and also serves static files."
而這個Command的執(zhí)行函數(shù)在這:
#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)
這里有關(guān)于use_reloader的判斷。如果我們在啟動命令中沒有加--noreload,程序就會走autoreload.main這個函數(shù),如果加了,就會走self.inner_run,直接啟動應(yīng)用。
其實從autoreload.main的參數(shù)也可以看出,它應(yīng)該是對self.inner_run做了一些封裝,autoreload的機(jī)制就在這些封裝當(dāng)中,下面我們繼續(xù)跟。
PS: 看源碼的時候發(fā)現(xiàn)django的command模式還是實現(xiàn)的很漂亮的,值得學(xué)習(xí)。
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做了區(qū)別處理,先忽略jpython;check_errors就是把對main_func進(jìn)行錯誤處理,也先忽略??磒ython_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
第一次走到這里時候,環(huán)境變量中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循環(huán), 內(nèi)部先把RUN_MAIN改成了"true",然后用os.spawnve方法開一個子進(jìn)程(subprocess),看看os.spawnve的說明:
#os.py def 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)
其實就是再調(diào)一遍命令行,又走了一遍 python manage.py runserver。
接著看restart_with_reloader里的while循環(huán),需要注意的是while循環(huán)退出的唯一條件是exit_code!=3。 如果子進(jìn)程不退出,就一直停在 os.spawnve這一步; 如果子進(jìn)程退出,而退出碼不是3,while就被終結(jié)了;如果是3,繼續(xù)循環(huán),重新創(chuàng)建子進(jìn)程。從這個邏輯可以猜想autoreload的機(jī)制:當(dāng)前進(jìn)程(主進(jìn)程)其實啥也不干,就監(jiān)視子進(jìn)程的運(yùn)行狀況,子進(jìn)程才是真正干事兒的;如果子進(jìn)程以exit_code=3退出(應(yīng)該由于檢測到了文件修改),就再啟動一遍子進(jìn)程,新代碼自然就生效了;如果子進(jìn)程以exit_code!=3退出,主進(jìn)程也結(jié)束,整個django程序就算跪了。這只是猜想,下面接著來驗證。
3、子進(jìn)程。上面其實有一個疑問,既然是重新啟動了一次,為什么子進(jìn)程不會接著生成子進(jìn)程?原因就在于RUN_MAIN這個環(huán)境變量,主進(jìn)程中把它改成了true,子進(jìn)程走到python_reloader函數(shù)的時候:
#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條件滿足了,和主進(jìn)程走了不一樣的邏輯分支。在這里,首先去開一個線程,運(yùn)行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 """ 官網(wǎng)說明: # 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_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系統(tǒng)文件處理的,先略過;
USE_INOTIFY也是系統(tǒng)文件操作相關(guān)的變量,根據(jù) inotify 是否可用選擇檢測文件變化的方法。
while循環(huán),每隔1秒檢測一下文件狀態(tài),如果是普通文件有變化,進(jìn)程退出,退出碼為3,主進(jìn)程一看:退出碼是3,就重啟子進(jìn)程。。。。這樣就和上面連上了;如果不是普通文件變化,而是I18N_MODIFIED(.mo后綴的文件變化,二進(jìn)制庫文件之類的),那就 reset_translations ,大概意思是把已加載過的庫緩存清理掉,下次重新加載。
以上就是autoreload機(jī)制的流程。其中還是有些細(xì)節(jié)不是特別清楚,比如不同操作系統(tǒng)文件變化的檢測,但都是很細(xì)節(jié)的東西了,不涉及主流程??赐赀@些,我又問了自己一遍,如果是讓我設(shè)計autoreload機(jī)制會怎樣搞?,F(xiàn)在我的答案是:直接把 django\utils\autoreload.py 文件拿來用啊。其實這是很獨(dú)立的一個模塊,而且特別通用,完全可以作為通用的autoreload解決方案,我還自己寫個毛啊。
這篇淺談django開發(fā)者模式中的autoreload是如何實現(xiàn)的就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中常見的反爬機(jī)制及其破解方法總結(jié)
今天給大家?guī)淼奈恼率顷P(guān)于Python的相關(guān)知識,文章圍繞著Python中常見的反爬機(jī)制及其破解方法展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06使用Python實現(xiàn)廣告點(diǎn)擊率預(yù)測
廣告點(diǎn)擊率是指有多少用戶點(diǎn)擊了您的廣告與有多少用戶查看了您的廣告的比率,本文主要為大家介紹了如何使用Python實現(xiàn)廣告點(diǎn)擊率預(yù)測,感興趣的小伙伴可以了解下2023-10-10Python中使用Queue和Condition進(jìn)行線程同步的方法
這篇文章主要介紹了Python中使用Queue模塊和Condition對象進(jìn)行線程同步的方法,配合threading模塊下的線程編程進(jìn)行操作的實例,需要的朋友可以參考下2016-01-01