現在,我們已經把一個Web App的框架完全搭建好了,從后端的API到前端的MVVM,流程已經跑通了。
在繼續工作前,注意到每次修改Python代碼,都必須在命令行先Ctrl-C停止服務器,再重啟,改動才能生效。
在開發階段,每天都要修改、保存幾十次代碼,每次保存都手動來這么一下非常麻煩,嚴重地降低了我們的開發效率。有沒有辦法讓服務器檢測到代碼修改后自動重新加載呢?
Django的開發環境在Debug模式下就可以做到自動重新加載,如果我們編寫的服務器也能實現這個功能,就能大大提升開發效率。
可惜的是,Django沒把這個功能獨立出來,不用Django就享受不到,怎么辦?
其實Python本身提供了重新載入模塊的功能,但不是所有模塊都能被重新載入。另一種思路是檢測www目錄下的代碼改動,一旦有改動,就自動重啟服務器。
按照這個思路,我們可以編寫一個輔助程序pymonitor.py,讓它啟動wsgiapp.py,并時刻監控www目錄下的代碼改動,有改動時,先把當前wsgiapp.py進程殺掉,再重啟,就完成了服務器進程的自動重啟。
要監控目錄文件的變化,我們也無需自己手動定時掃描,Python的第三方庫watchdog可以利用操作系統的API來監控目錄文件的變化,并發送通知。我們先用easy_install安裝:
1
|
$ easy_install watchdog |
利用watchdog接收文件變化的通知,如果是.py文件,就自動重啟wsgiapp.py進程。
利用Python自帶的subprocess實現進程的啟動和終止,并把輸入輸出重定向到當前進程的輸入輸出中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#!/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行左右的代碼,就實現了Debug模式的自動重新加載。用下面的命令啟動服務器:
1
|
$ python pymonitor.py wsgiapp.py |
或者給pymonitor.py加上可執行權限,啟動服務器:
1
|
$ . /pymonitor .py wsgiapp.py |
在編輯器中打開一個py文件,修改后保存,看看命令行輸出,是不是自動重啟了服務器:
1
2
3
4
5
6
7
8
9
10
11
12
|
$ . / 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 |