用Python實(shí)現(xiàn)服務(wù)器中只重載被修改的進(jìn)程的方法
現(xiàn)在,我們已經(jīng)把一個(gè)Web App的框架完全搭建好了,從后端的API到前端的MVVM,流程已經(jīng)跑通了。
在繼續(xù)工作前,注意到每次修改Python代碼,都必須在命令行先Ctrl-C停止服務(wù)器,再重啟,改動(dòng)才能生效。
在開發(fā)階段,每天都要修改、保存幾十次代碼,每次保存都手動(dòng)來(lái)這么一下非常麻煩,嚴(yán)重地降低了我們的開發(fā)效率。有沒(méi)有辦法讓服務(wù)器檢測(cè)到代碼修改后自動(dòng)重新加載呢?
Django的開發(fā)環(huán)境在Debug模式下就可以做到自動(dòng)重新加載,如果我們編寫的服務(wù)器也能實(shí)現(xiàn)這個(gè)功能,就能大大提升開發(fā)效率。
可惜的是,Django沒(méi)把這個(gè)功能獨(dú)立出來(lái),不用Django就享受不到,怎么辦?
其實(shí)Python本身提供了重新載入模塊的功能,但不是所有模塊都能被重新載入。另一種思路是檢測(cè)www目錄下的代碼改動(dòng),一旦有改動(dòng),就自動(dòng)重啟服務(wù)器。
按照這個(gè)思路,我們可以編寫一個(gè)輔助程序pymonitor.py,讓它啟動(dòng)wsgiapp.py,并時(shí)刻監(jiān)控www目錄下的代碼改動(dòng),有改動(dòng)時(shí),先把當(dāng)前wsgiapp.py進(jìn)程殺掉,再重啟,就完成了服務(wù)器進(jìn)程的自動(dòng)重啟。
要監(jiān)控目錄文件的變化,我們也無(wú)需自己手動(dòng)定時(shí)掃描,Python的第三方庫(kù)watchdog可以利用操作系統(tǒng)的API來(lái)監(jiān)控目錄文件的變化,并發(fā)送通知。我們先用easy_install安裝:
$ easy_install watchdog
利用watchdog接收文件變化的通知,如果是.py文件,就自動(dòng)重啟wsgiapp.py進(jìn)程。
利用Python自帶的subprocess實(shí)現(xiàn)進(jìn)程的啟動(dòng)和終止,并把輸入輸出重定向到當(dāng)前進(jìn)程的輸入輸出中:
#!/usr/bin/env python import os, sys, time, subprocess from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler def log(s): print '[Monitor] %s' % s class MyFileSystemEventHander(FileSystemEventHandler): def __init__(self, fn): super(MyFileSystemEventHander, self).__init__() self.restart = fn def on_any_event(self, event): if event.src_path.endswith('.py'): log('Python source file changed: %s' % event.src_path) self.restart() command = ['echo', 'ok'] process = None def kill_process(): global process if process: log('Kill process [%s]...' % process.pid) process.kill() process.wait() log('Process ended with code %s.' % process.returncode) process = None def start_process(): global process, command log('Start process %s...' % ' '.join(command)) process = subprocess.Popen(command, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr) def restart_process(): kill_process() start_process() def start_watch(path, callback): observer = Observer() observer.schedule(MyFileSystemEventHander(restart_process), path, recursive=True) observer.start() log('Watching directory %s...' % path) start_process() try: while True: time.sleep(0.5) except KeyboardInterrupt: observer.stop() observer.join() if __name__ == '__main__': argv = sys.argv[1:] if not argv: print('Usage: ./pymonitor your-script.py') exit(0) if argv[0]!='python': argv.insert(0, 'python') command = argv path = os.path.abspath('.') start_watch(path, None)
一共50行左右的代碼,就實(shí)現(xiàn)了Debug模式的自動(dòng)重新加載。用下面的命令啟動(dòng)服務(wù)器:
$ python pymonitor.py wsgiapp.py
或者給pymonitor.py加上可執(zhí)行權(quán)限,啟動(dòng)服務(wù)器:
$ ./pymonitor.py wsgiapp.py
在編輯器中打開一個(gè)py文件,修改后保存,看看命令行輸出,是不是自動(dòng)重啟了服務(wù)器:
$ ./pymonitor.py wsgiapp.py [Monitor] Watching directory /Users/michael/Github/awesome-python-webapp/www... [Monitor] Start process python wsgiapp.py... ... INFO:root:application (/Users/michael/Github/awesome-python-webapp/www) will start at 0.0.0.0:9000... [Monitor] Python source file changed: /Users/michael/Github/awesome-python-webapp/www/apis.py [Monitor] Kill process [2747]... [Monitor] Process ended with code -9. [Monitor] Start process python wsgiapp.py... ... INFO:root:application (/Users/michael/Github/awesome-python-webapp/www) will start at 0.0.0.0:9000... Try
相關(guān)文章
Python類和對(duì)象的定義與實(shí)際應(yīng)用案例分析
這篇文章主要介紹了Python類和對(duì)象的定義與實(shí)際應(yīng)用,結(jié)合三個(gè)具體案例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類與對(duì)象的定義、應(yīng)用、設(shè)計(jì)模式等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12Python創(chuàng)建多線程的兩種常用方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Python中創(chuàng)建多線程的兩種常用方法,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們掌握Python有一定的幫助,需要的可以收藏一下2023-05-05PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained)
今天小編就為大家分享一篇PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python OpenGL繪制一場(chǎng)煙花盛會(huì)
正值新春佳節(jié),小編今天為大家?guī)?lái)了用Python OpenGL繪制的一場(chǎng)煙花盛會(huì),文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-02-02用python打開攝像頭并把圖像傳回qq郵箱(Pyinstaller打包)
這篇文章主要介紹了用python打開攝像頭并把圖像傳回qq郵箱,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05自己編程中遇到的Python錯(cuò)誤和解決方法匯總整理
這篇文章主要介紹了自己編程中遇到的Python錯(cuò)誤和解決方法匯總整理,本文收集整理了較多的案例,需要的朋友可以參考下2015-06-06