flask 定时任务(APScheduler)使用current_app app_context()上下文

前言:

描述:flask定时任务调用的方法中使用了current_app.logger.info()记录日志报错

 

报错代码

   raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

解决办法 :

1.创建 create.py文件

为了方便快速使用此代码,我把create.py非核心代码已注释掉,如下:

from flask import Flask, json

# from utils.cache_helper import CacheHelper
# from utils.log_handler import LogHandler


def create_app():
    # init app
    app = Flask(__name__)
    # # 读取json配置
    # app.config.from_file("settings.json", load=json.load)
    # # 初始化Cache
    # cache = CacheHelper(app, config={'CACHE_TYPE': 'simple'})
    # # 添加日志配置
    # for log_handler in LogHandler.get_log_handlers():
    #     app.logger.addHandler(log_handler)
    return app

  2.定时任务调用的方法中代码如下 

引入create.py文件中create_app()方法
from create import create_app
方法一
   @classmethod
    def my_job(cls):
        # 此方法在定时任务多的情况下,会有性能问题,少的情况没啥问题
        app = create_app()
        with app.app_context():
            current_app.logger.info("my_job已执行")
            print(f"my_job,当前时间{datetime.now()}")
方法二 (推荐)

添加APP全局变量

from create import create_app

APP = None


def get_app():
    global APP
    APP = APP if APP is not None else create_app()

定时任务调用的方法中使用如下

提示with APP.app_context():不要写成with APP.app_context:    会报错,因为app_context是一个方法而不是一个属性,所以要写成app_context(),加上括号

 @classmethod
    def my_job(cls):      
        # 使用全局APP变量
        get_app()
        with APP.app_context():
            current_app.logger.info("my_job已执行")
            print(f"my_job,当前时间{datetime.now()}")

3.执行效果

总结:

flask定时任务(APScheduler)的使用,链接如下: https://blog.csdn.net/weixin_41934979/article/details/140245835

结合上边链接,就是完整的flask 定时任务(APScheduler)使用current_app的全过程和步骤

 源代码地址:https://gitee.com/jxzcode_admin/flask-project.git

参考资料:

https://blog.csdn.net/weixin_42185136/article/details/104496351?spm=1001.2014.3001.5506

https://www.jianshu.com/p/d5a46b2d2fd3 

相关推荐

  1. flask_apscheduler 定时任务框架

    2024-07-18 04:22:02       30 阅读
  2. Django 使用Apscheduler执行定时任务

    2024-07-18 04:22:02       24 阅读
  3. flask-apscheduler 定时任务被执行两次

    2024-07-18 04:22:02       23 阅读
  4. 【Python笔记-FastAPI】定时任务实现(APScheduler

    2024-07-18 04:22:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 04:22:02       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 04:22:02       56 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 04:22:02       46 阅读
  4. Python语言-面向对象

    2024-07-18 04:22:02       57 阅读

热门阅读

  1. 无需安装jadx-gui,获取app公钥和MD5

    2024-07-18 04:22:02       19 阅读
  2. elasticsearch源码分析-05分片分配

    2024-07-18 04:22:02       14 阅读
  3. 营销策划方案怎么写?

    2024-07-18 04:22:02       17 阅读
  4. 中国高端水果元宇宙

    2024-07-18 04:22:02       16 阅读
  5. 牛客多校暑期第一场

    2024-07-18 04:22:02       16 阅读
  6. 记一次Mysql连接失败的处理过程

    2024-07-18 04:22:02       26 阅读
  7. 从入门到高手的99个python案例

    2024-07-18 04:22:02       17 阅读
  8. Springboot Excel 导出工具 -- EasyPoi 简介

    2024-07-18 04:22:02       20 阅读
  9. 智能家居的优缺点有哪些?

    2024-07-18 04:22:02       15 阅读