代码拉取完成,页面将自动刷新
import os
import subprocess
import sys
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
def log(s):
print('[Monitor] %s' % s)
class MyFileSystemEventHander(FileSystemEventHandler):
def __init__(self, fn):
super(MyFileSystemEventHander, self).__init__()
self.restart = fn
# 完成修改后启动
def on_modified(self, event):
if event.src_path.endswith('.py'):
log('Python source file changed: %s' % event.src_path)
self.restart()
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 = []
# 加入Python 命令符
command.append("python")
appPath = os.path.abspath("./PythonWebApp/www/app.py")
print(appPath)
command.append(appPath)
log('Start process command -> %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)
log('Watching directory %s...' % path)
observer.start()
# 开始执行入口文件
start_process()
try:
while True:
time.sleep(0.5)
except KeyboardInterrupt:
observer.stop()
# 检测到键盘 ctrl C 终止命令后结束
observer.join()
if __name__ == '__main__':
print(os.path.abspath("."))
# 在当前目录的子目录中进行监视
path = os.path.abspath('./PythonWebApp/www/')
print(os.path.exists(path))
start_watch(path, None)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。