将芹菜与Flask应用程序上下文一起使用会产生“错误的应用程序上下文错误".断言错误 [英] Using celery with Flask app context gives "Popped wrong app context." AssertionError

查看:42
本文介绍了将芹菜与Flask应用程序上下文一起使用会产生“错误的应用程序上下文错误".断言错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我或多或少使用设置从您的烧瓶应用程序上下文中运行Celery任务: http://flask.pocoo.org/docs/0.10/patterns/celery/

I'm more or less using the setup to run Celery tasks using your flask app context from here: http://flask.pocoo.org/docs/0.10/patterns/celery/

我收到与创建,在Flask应用中管理和杀死后台任务

但是,我正在执行Celery任务的实际工人中得到它.这是跟踪:

but, I'm getting it in the actual worker where the Celery task is being executed. Here is the trace:

worker_1 | Traceback (most recent call last):
worker_1 |   File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task
worker_1 |     R = retval = fun(*args, **kwargs)
worker_1 |   File "/code/app/__init__.py", line 42, in __call__
worker_1 |     return TaskBase.__call__(self, *args, **kwargs)
worker_1 |   File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 186, in __exit__
worker_1 |     self.pop(exc_value)
worker_1 |   File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 178, in pop
worker_1 |     % (rv, self)
worker_1 | AssertionError: Popped wrong app context.  (<flask.ctx.AppContext object at 0x47a5790> instead of <flask.ctx.AppContext object at 0x427e390>)

有人有什么想法吗?在应用程序上下文中运行应该可以解决此问题,而不是创建它!

Anyone have any ideas? Running in the app context should be fixing this problem, not creating it!

谢谢

推荐答案

我知道距您发布已经有4年了,但是我正在使用 rq 而不是 celery进行类似的问题解答.

I know this is 4 years since you posted, but I was battling with a similar problem excep using rq not celery.

网络上每个人都告诉您,您需要向应用程序添加上下文,例如,按照

What everyone on the web tells you is that you need to add context to your app, eg as per the Supertutorial:

app = create_app()
app.app_context().push()

他们没有告诉您的是,如果此文件中还有其他任何东西会导致您的应用主实例打开它(例如另一个功能),那么您将点击上面的两行并获得错误.

What they don't tell you is that if you have anything else in this file that will cause your primary instance of the app to open it (Eg another function), then you'll hit the 2 lines above and get the error.

我的解决方案是将两行放在我要分派给这样的工作程序的实际异步函数中:

My solution was to put the 2 lines inside of the actual async function that I'm offloading to a worker like this:

def run_async_backup(machine_id):
    app = create_app()
    app.app_context().push()
    # your code for the worker...

这意味着主实例将不会命中它们,而辅助对象将被击中(假定只有辅助对象调用了此函数).

This means the primary instance won't hit them - while the worker will (assume only the worker calls this function).

这篇关于将芹菜与Flask应用程序上下文一起使用会产生“错误的应用程序上下文错误".断言错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆