falsk 自动重载html,如何使用Flask + uWSGI设置自动重载?

twooster..

5

开发模式Flask的自动重新加载功能实际上是由底层的Werkzeug库提供的.相关代码是werkzeug/serving.py- 值得一看.但基本上,主应用程序将WSGI服务器生成为一个子进程,每秒对每个活动.py文件进行一次统计,查找更改.如果它看到任何,则子进程退出,并且父进程再次启动它 - 实际上重新加载chages.

你无法在uWSGI层实现类似的技术.如果您不想使用stat循环,可以尝试使用底层OS file-watch命令.显然(根据Werkzeug的代码),pyinotify是错误的,但也许看门狗工作?尝试一些事情,看看会发生什么.

编辑:

在回应评论时,我认为这很容易重新实现.以您的链接提供的示例为基础,以及来自的代码werkzeug/serving.py:

""" NOTE: _iter_module_files() and check_for_modifications() are both

copied from Werkzeug code. Include appropriate attribution if

actually used in a project. """

import uwsgi

from uwsgidecorators import timer

import sys

import os

def _iter_module_files():

for module in sys.modules.values():

filename = getattr(module, '__file__', None)

if filename:

old = None

while not os.path.isfile(filename):

old = filename

filename = os.path.dirname(filename)

if filename == old:

break

else:

if filename[-4:] in ('.pyc', '.pyo'):

filename = filename[:-1]

yield filename

@timer(3)

def check_for_modifications():

# Function-static variable... you could make this global, or whatever

mtimes = check_for_modifications.mtimes

for filename in _iter_module_files():

try:

mtime = os.stat(filename).st_mtime

except OSError:

continue

old_time = mtimes.get(filename)

if old_time is None:

mtimes[filename] = mtime

continue

elif mtime > old_time:

uwsgi.reload()

return

check_for_modifications.mtimes = {} # init static

这是未经测试的,但应该有效.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值