import osimport subprocessimport sysimport timefrom watchdog.events import FileSystemEventHandlerfrom watchdog.observers import Observerdef 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 = Nonedef kill_process():global processif process:log('Kill process [%s]...' % process.pid)process.kill()process.wait()log('Process ended with code %s.' % process.returncode)process = Nonedef start_process():global processcommand = []# 加入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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。