web框架中的代码自动重载怎么实现

在开发和调试wsgi应用程序时,有很多方法可以自动重新加载代码。例如,如果你使用的是werkzeug,则只需要传use_reloader参数即可:

run_sumple('127.0.0.1', 5000, app, use_reloader=True)

对于Flask,实际上在内部使用werkzeug,所以你需要设置debug = true:

app.run(debug=True)

django会在你修改任何代码的时候自动为你重新加载:

python manage.py runserver

所有这些例子在本地开发的时候都非常有用,但是,建议不要在实际生产中使用。

作为学习,可以一起来看一下,python是如何让代码自动地重新加载的?

uWSGI

如果使用的是 uwsgi 和 django ,实际上可以直接通过代码跳转查看一下 django 本身的自动重载机制:

import uwsgi
from uwsgidecorators import timer
from django.utils import autoreload

@timer(3)
def change_code_gracefull_reload(sig):
    if autoreload.code_changed():
        uwsgi.reload()

可以看出,django 通过一个定时器在不断的监测代码是否有变化,以此来出发重新加载函数。

如果你使用的是其他框架,或者根本没有框架,那么可能就需要在应用程序中自己来实现代码改动监测。这里是一个很好的示例代码,借用和修改自 cherrypy

复制代码
import os, sys

_mtimes = {}
_win = (sys.platform == "win32")

_error_files = []

def code_changed():
    filenames = []
    for m in sys.modules.values():
        try:
            filenames.append(m.__file__)
        except AttributeError:
            pass
    for filename in filenames + _error_files:
        if not filename:
            continue
        if filename.endswith(".pyc") or filename.endswith(".pyo"):
            filename = filename[:-1]
        if filename.endswith("$py.class"):
            filename = filename[:-9] + ".py"
        if not os.path.exists(filename):
            continue # File might be in an egg, so it can't be reloaded.
        stat = os.stat(filename)
        mtime = stat.st_mtime
        if _win:
            mtime -= stat.st_ctime
        if filename not in _mtimes:
            _mtimes[filename] = mtime
            continue
        if mtime != _mtimes[filename]:
            _mtimes.clear()
            try:
                del _error_files[_error_files.index(filename)]
            except ValueError:
                pass
            return True
    return False
复制代码

你可以将上面的内容保存在你的项目中的 autoreload.py 中,然后我们就可以像下面这样去调用它(类似于 django 的例子):

import uwsgi
from uwsgidecorators import timer
import autoreload

@timer(3)
def change_code_gracefull_reload(sig):
    if autoreload.code_changed():
        uwsgi.reload()

gunicorn

对于 gunicorn ,我们需要写一个脚本来 hook(钩子:触发) 到 gunicorn 的配置中:

复制代码
import threading
import time
try:
    from django.utils import autoreload
except ImportError:
    import autoreload

def reloader(server):
    while True:
        if autoreload.code_changed():
            server.reload()
        time.sleep(3)

def when_ready(server):
    t = threading.Thread(target=reloader, args=(server, ))
    t.daemon = True
    t.start()
复制代码

你需要把上面的代码保存到一个文件中,比如说 config.py ,然后像下面这样传给 gunicorn :

gunicorn -c config.py application

外部解决方法

你也可以通过正在使用的 wsgi 服务系统本身以外的一些方法来实现重启系统,它只需发出一个信号,告诉系统重启代码,比如可以使用 watchdog。例如:

watchmedo shell-command --patterns="*.py" --recursive --command='kill -HUP `cat /tmp/gunicorn.pid`' /path/to/project/

原文链接: http://www.vimiix.com/post/2018/01/08/autoreload-code-in-python/

posted on   不要挡着我晒太阳  阅读(310)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示