1 Star 0 Fork 0

夏至如沫/Ex_Python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pymonitor.py 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
MiaoShuaitao 提交于 2017-12-26 18:08 +08:00 . 更新完成版本
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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/Toater_OSC/Ex_Python.git
git@gitee.com:Toater_OSC/Ex_Python.git
Toater_OSC
Ex_Python
Ex_Python
master

搜索帮助