(十三)自動載入新模組

weixin_34138377發表於2018-01-17

Python的第三方庫watchdog可以利用作業系統的API來監控目錄檔案的變化,併傳送通知。先用pip安裝:

$ pip3 install watchdog

編寫一個輔助程式pymonitor.py,讓它啟動app.py,並時刻監控www目錄下的程式碼改動,有改動時,先把當前wsgiapp.py程式殺掉,再重啟:

#!/usr/bin/env python3

import os, sys, time, subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

def log(s):
    print('[Monitor] %s' % s)

class MyFileSystemEventHandler(FileSystemEventHandler):

    def __init__(self, fn):
        super(MyFileSystemEventHandler, 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']    # 任意初始化兩個字串, 只是為了表明command一般由兩段組成
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語句, 為定義在函式外的變數賦值, 或明確將要使用的變數是全域性的而不是區域性定義的
    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類實現了監控檔案變化觸發對應事件的類, 然後呼叫關聯的事件處理類來處理事件
    # 該類其實是threading.Thread的子類, 通過observer.start()使之執行在一個執行緒中, 不會阻塞主程式執行
    # 然後可以呼叫observer.stop()來停止該執行緒
    observer = Observer()
    # observer.schedule(event_handler,path,recursive=False)監控指定路徑path
    # 該路徑觸發任何事件都會呼叫event_handler來處理, 如果path是目錄, recursive=True則會遞迴監控該目錄的所有變化
    # 當path變化時, 就會產生一個特定事件, 也就是event類的子類, 包含屬性event_type,src_path,is_directory
    observer.schedule(MyFileSystemEventHandler(restart_process), path, recursive=True)
    observer.start()
    log('Watching directioy %s...' % path)
    start_process()
    try:
        while True:
            time.sleep(0.5)
    except KeyboardInterrupt:
        observer.stop()
    # 阻塞正在呼叫的執行緒,直到被呼叫join()方法的執行緒結束
    observer.join()

if __name__ == '__main__':
    argv = sys.argv[1:]    # 用法./pymonitor.py app.py
    if not argv:
        print('Usage: ./pymonitor your_script.py')
        exit(0)    # 正常退出
    if argv[0] != 'python3':
        argv.insert(0, 'python3')
    command = argv
    path = os.path.abspath('.')    # 當前目錄
    start_watch(path, None)

給pymonitor.py加上可執行許可權:

$ chmod u+x pymonitor.py

啟動伺服器:

$ ./pymonitor.py app.py

這裡會報錯不知道為什麼:

/usr/bin/env: ‘python3\r’: No such file or directory

目測是window轉到linux上\r的問題,但是怎們也消除不掉,先換另一種方法:

python3 pymonitor.py app.py

在編輯器中開啟一個.py檔案,修改後儲存,看看命令列輸出,是不是自動重啟了伺服器:

myweb@ubuntu:~/py3-webapp/www$ python3 pymonitor.py app.py
[Monitor] Watching directioy /home/myweb/py3-webapp/www...
[Monitor] Start process python3 app.py...
INFO:root:Found model: User (table: users)
...
INFO:root:Server started at http://192.168.179.140:7000...
# 我在這時修改了一個檔案
[Monitor] Python source file changed: /home/myweb/py3-webapp/www/apis.py
[Monitor] Kill process [1308]...
[Monitor] Process ended with code -9.
[Monitor] Start process python3 app.py...
[Monitor] Python source file changed: /home/myweb/py3-webapp/www/apis.py
[Monitor] Kill process [1312]...
[Monitor] Process ended with code -9.
[Monitor] Start process python3 app.py...
[Monitor] Python source file changed: /home/myweb/py3-webapp/www/apis.py
[Monitor] Kill process [1313]...
[Monitor] Process ended with code -9.
[Monitor] Start process python3 app.py...
INFO:root:Found model: User (table: users)
...
INFO:root:Server started at http://192.168.179.140:7000...

相關文章