不要重新加载所有Flask应用程序

时间:2017-09-03 10:34:34

标签: flask

我正在使用烧瓶蓝图,如下所示: http://flask.pocoo.org/docs/0.12/patterns/appdispatch/

具体来说,我正在运行以下代码:

from werkzeug.wsgi import DispatcherMiddleware
from frontend_app import application as frontend
from backend_app import application as backend

application = DispatcherMiddleware(frontend, {
    '/backend':     backend
})

Flask具有重新加载应用程序的强大功能,因为它检测到代码更改。但是,在我的情况下,两个应用程序都不可取:后端应用程序太重,无法每隔几分钟重新加载一次。当我重新加载fronend时,后端也会重新加载,这需要很长时间。有没有办法只重新加载已更改的应用程序?

我意识到这可能很难,因为据我所知,

application = DispatcherMiddleware(frontend, {
    '/backend':     backend
})

应用程序合并为一个应用程序,它们不再分开。

无论如何,也许有不同的方式来实现我想要的目标?

感谢。

1 个答案:

答案 0 :(得分:0)

我不确定是否有任何直接做这样的事情。如果您阻止重新初始化模块,我只能想到。

请参阅以下示例,了解如何实现此目标

<强> reloadme.py

SET @dr = 0;
SET @points = NULL;
SET @team = NULL;

SELECT
    s.dr,
    s.points,
    t.name AS team_name,
    d.name AS discipline_name
FROM
(
    SELECT
        @dr:=CASE WHEN @team = team AND @points <> points THEN @dr + 1
                  WHEN @team = team AND @points = points THEN @dr
                  ELSE 1 END AS dr,
        @points:=points AS points,
        @team:=team AS team,
        discipline
    FROM scores s
    ORDER BY
        s.team,
        s.points DESC
) s
INNER JOIN teams t
    ON s.team = t.id
INNER JOIN disciplines d
    ON s.discipline = d.id
WHERE s.dr <= 3
ORDER BY
    s.team,
    s.points DESC;

<强> reloadtest.py

import sys

if 'reloadme' in sys.modules and hasattr(sys.modules['reloadme'], '__MODULE_LOADED'):
    print ("module is being reloaded")
    mod = sys.modules['reloadme']
    x = mod.x
else:
    print ("Loading module for first time")
    __MODULE_LOADED = True
    x = 2

def printx():
    print (x)

运行from importlib import reload import reloadme reloadme.printx() reloadme.x = 10 reloadme.printx() print("Reloading reloadme to check if x is changed back to 2") reload(reloadme) reloadme.printx() 的输出将如下所示

reloadtest.py