Python 如何跟踪其值在应用程序的整个生命周期中不断变化的变量?
我有一个Flask应用程序,其中有3个端点用于管理Flask应用程序。有一个变量Python 如何跟踪其值在应用程序的整个生命周期中不断变化的变量?,python,python-3.x,flask,global-variables,flask-appbuilder,Python,Python 3.x,Flask,Global Variables,Flask Appbuilder,我有一个Flask应用程序,其中有3个端点用于管理Flask应用程序。有一个变量运行状况,其值最初为“UP”-字符串 /check=检查烧瓶应用程序的状态。不管是上升还是下降 /up=将变量的值更改为“up”,其值在服务任何请求之前用作检查 /down=将变量的值更改为“down” 当运行状况\u状态为“UP”时,应用程序可以为其提供的任何端点提供服务。当它为“DOWN”时,它只会返回返回返回服务器健康状态的任何API端点excep/up端点的500错误(我正在使用Flask中的@app.bef
运行状况
,其值最初为“UP”-字符串
/check=检查烧瓶应用程序的状态。不管是上升还是下降
/up=将变量的值更改为“up”,其值在服务任何请求之前用作检查
/down=将变量的值更改为“down”
当运行状况\u状态为“UP”时,应用程序可以为其提供的任何端点提供服务。当它为“DOWN”时,它只会返回返回返回服务器健康状态的任何API端点excep/up端点的500错误(我正在使用Flask中的@app.before\u request
执行任何API调用之前进行检查)
我想知道的是这是否更可取。有没有其他办法来完成这项任务
健康检查。py:
from flask.json import jsonify
from app.common.views.api_view import APIView
from app import global_config
class View(APIView):
def check(self):
return jsonify({'status': f"Workload service is {global_config.health_status}"})
def up(self):
global_config.health_status = "UP"
return jsonify({'status': "Workload service is up and running"})
def down(self):
global_config.health_status = "DOWN"
return jsonify({'status': f"Workload service stopped"})
workload_health_status = "UP"
from flask import Flask, request, jsonify
from app import global_config
excluded_paths = ['/api/health/up/', '/api/health/down/']
def register_blueprints(app):
from .health import healthcheck_api
app.register_blueprint(healthcheck_api, url_prefix="/api/health")
def create_app(**kwargs):
app = Flask(__name__, **kwargs)
register_blueprints(app)
@app.before_request
def health_check_test():
if request.path not in excluded_paths and global_config.workload_health_status == "DOWN":
return jsonify({"status": "Workload service is NOT running"}), 500
return app
global\u config.py:
from flask.json import jsonify
from app.common.views.api_view import APIView
from app import global_config
class View(APIView):
def check(self):
return jsonify({'status': f"Workload service is {global_config.health_status}"})
def up(self):
global_config.health_status = "UP"
return jsonify({'status': "Workload service is up and running"})
def down(self):
global_config.health_status = "DOWN"
return jsonify({'status': f"Workload service stopped"})
workload_health_status = "UP"
from flask import Flask, request, jsonify
from app import global_config
excluded_paths = ['/api/health/up/', '/api/health/down/']
def register_blueprints(app):
from .health import healthcheck_api
app.register_blueprint(healthcheck_api, url_prefix="/api/health")
def create_app(**kwargs):
app = Flask(__name__, **kwargs)
register_blueprints(app)
@app.before_request
def health_check_test():
if request.path not in excluded_paths and global_config.workload_health_status == "DOWN":
return jsonify({"status": "Workload service is NOT running"}), 500
return app
app/\uuuu init.py:
from flask.json import jsonify
from app.common.views.api_view import APIView
from app import global_config
class View(APIView):
def check(self):
return jsonify({'status': f"Workload service is {global_config.health_status}"})
def up(self):
global_config.health_status = "UP"
return jsonify({'status': "Workload service is up and running"})
def down(self):
global_config.health_status = "DOWN"
return jsonify({'status': f"Workload service stopped"})
workload_health_status = "UP"
from flask import Flask, request, jsonify
from app import global_config
excluded_paths = ['/api/health/up/', '/api/health/down/']
def register_blueprints(app):
from .health import healthcheck_api
app.register_blueprint(healthcheck_api, url_prefix="/api/health")
def create_app(**kwargs):
app = Flask(__name__, **kwargs)
register_blueprints(app)
@app.before_request
def health_check_test():
if request.path not in excluded_paths and global_config.workload_health_status == "DOWN":
return jsonify({"status": "Workload service is NOT running"}), 500
return app
您可以使用应用程序的内置功能,从应用程序中的任何位置查询/更新它,例如app.config['health\u status']=“UP”
。这样就不需要使用global\u config
对象。使用@app.before_request
可能仍然是检查此值的最优雅的方法。如果应用程序正常出错,用户难道不会完全知道它是否已关闭吗?如果应用程序实际已关闭,/up
或/check
如何更改任何内容?我只是设置一个变量,在调用/check
端点时显示其值。以及在调用/up
或/down
时修改变量值。我还需要有一种方法在调用/up
端点后启动应用程序并为API提供服务。对,但如果应用程序处于关闭状态,这两种路由实际上都不起作用。它正在工作。因为我也在检查路径是否在@app.before_request中的排除的_路径
列表中。我如何访问app.config['health_status']
中的health_check.py
?使用flask导入当前的_应用程序作为app
,然后使用app.config['workload\u health_status']
,是,或者您可以直接引用当前的应用程序配置。不需要别名,明白了。谢谢。:-)