Python 蟒蛇、芹菜、烧瓶;“在应用程序上下文之外工作”;

Python 蟒蛇、芹菜、烧瓶;“在应用程序上下文之外工作”;,python,flask,celery,Python,Flask,Celery,我正在尝试使用芹菜和Python为Flask应用程序安排任务。我基本上希望每隔x个时间在另一个目录中运行一个函数,并将其作为芹菜任务。我导入函数test_check,并尝试将其放在一个名为testcheck()的芹菜任务下,但是,我得到了错误: 在应用程序上下文之外工作 我怎样才能解决这个问题?以下是我的设置: from app import app from celery import Celery from datetime import timedelta from app.mod_che

我正在尝试使用芹菜和Python为Flask应用程序安排任务。我基本上希望每隔x个时间在另一个目录中运行一个函数,并将其作为芹菜任务。我导入函数test_check,并尝试将其放在一个名为testcheck()的芹菜任务下,但是,我得到了错误:

在应用程序上下文之外工作

我怎样才能解决这个问题?以下是我的设置:

from app import app
from celery import Celery
from datetime import timedelta
from app.mod_check.views import test_check

celery = Celery(__name__,
             broker='amqp://guest:@localhost/',
             backend='amqp://guest:@localhost/'
             )

celery.config_from_object(__name__)

@celery.task
def add(x, y):
    print "celery working!"
    return x + y

@celery.task
def testcheck():
        test_check()

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks2.testcheck',
        'schedule': timedelta(seconds=5),
        #'args': (16, 16)
    },
}

CELERY_TIMEZONE = 'Europe/London'

无论
test\u check
是什么,它都需要一个请求上下文。由于芹菜任务不是HTTP请求/响应周期的一部分,因此需要手动设置请求上下文

with app.test_request_context():
    test_check()

你看到了吗?@jonafato用这种方法我得到了
AttributeError:'NoneType'对象没有属性'is\u authenticated'