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

栏目: Python · 发布时间: 8年前

内容简介:[python]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

如果使用的是 uwsgidjango ,实际上可以直接通过代码跳转查看一下 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/


以上所述就是小编给大家介绍的《[python]web框架中的代码自动重载怎么实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

读屏时代

读屏时代

(美)Naomi S. Baron(内奥米·S.巴伦) / 庞洋 / 电子工业出版社 / 2016-7 / 55.00

书中作者探讨了技术如何重塑人们对阅读的定义。数字阅读越来越受欢迎,更便利、节约成本、并把免费书籍提供给全世界的读者。但是,作者也指出其弊处在于读者很容易被设备上的其他诱惑分心、经常走马观花而非深入阅读。更重要的是,人们阅读方式的变化会影响了作者的写作方式。为了迎合人们阅读习惯的转变,许多作家和出版商的作品越来越短小和碎片化,或者更青睐无需思考和细读的作品。作者比较了纸质阅读和在线阅读的重要性,包括......一起来看看 《读屏时代》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试