Flask-apscheduler获取或修改上下文中config数据

发布时间 2024-01-10 14:31:41作者: Swlymbcty

__init__.py

 

 1 from flask_apscheduler import APScheduler
 2 ...
 3 
 4 scheduler = APScheduler()
 5 
 6  7 from app import app
 8 
 9 
10 
11 
12 
13 
14 def create_app(config_name):
15     app = Flask(__name__)
16 
17     CORS(app, resources=r'/*')
18 
19     # 根据配置模式的名字获取配置参数的类
20     config_class = config_map.get(config_name)
21     app.config.from_object(config_class)
22 
23     # 启动定时器
24     scheduler.init_app(app)
25     scheduler.start()
26     ...
27 
28     return app

 

 

test.py

1 from . import scheduler
2 
3 @scheduler.task('interval', id='aa', seconds=300, next_run_time=datetime.datetime.now())
4 def aa():
5     with scheduler.app.app_context():
6         print(scheduler.app.config["config_data"])
7         scheduler.app.config["config_data"] = ...
8