【问题标题】:Flask context for RQ jobs (RuntimeError: Working outside of application)RQ 作业的 Flask 上下文(RuntimeError:在应用程序之外工作)
【发布时间】:2021-09-17 14:00:01
【问题描述】:

首先,关于flask_context的问题,包括RQ作业的上下文似乎是常见问题,但我搜索了很多,仍然无法解决我的问题。

我的装饰器功能(在不同的变体中尝试了它们):

def redis_decorator1(f):
    def inner_wrapper(*args, **kwargs):
        # db interactions
        return res
    return inner_wrapper

def redis_decorator2(app):
    # app.app_context().push()
    def wrapper(f):
        def inner_wrapper(*args, **kwargs):
            # db interactions
            return res
        return inner_wrapper
    return wrapper



...
@redis_decorator
def under_decorator_func(*some_args)

在函数under_decorator_func 中使用flask.current_app

问题:

First decorator -  RuntimeError: "Working outside of application
context" when redis task starts.

Second decorator - Same error on app start

我也在创建应用程序后立即尝试了app.app_context().push(),所以我不知道这里发生了什么。

【问题讨论】:

    标签: flask redis python-rq rq


    【解决方案1】:

    原来解决方案非常明显,需要更多关于 python 上下文的知识:

    app = create_app() # or anything that creates your Flask application
    
    ...
    
    
    def redis_decorator2(app):
    def wrapper(f):
        def inner_wrapper(*args, **kwargs):
            with app.app_context():
                # db interactions
                return res
        return inner_wrapper
    return wrapper
    

    重要的是在inner_wrapper 函数中使用flask context,而不是上面的任何层,否则你会得到和以前一样的错误。 希望它会对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-06
      • 2020-03-06
      • 2015-10-05
      • 1970-01-01
      • 2016-03-11
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多