在wsgi应用程序中访问app singleton实例

时间:2015-09-28 17:43:53

标签: python flask celery wsgi gunicorn

我想定义一些芹菜任务。这是我的tasks.py模块:

from celery import Celery


def create_celery_app(app):
    celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)

    celery.Task = ContextTask
    return celery


celery = create_celery_app(app)  # <--- How to access app here?

@celery.task()
def add_together(a, b):
    return a + b

问题是,我无法访问app这里!我的应用是在我的wsgi.py模块中定义的(我正在使用gunicorn

from myproj.app import create_app

app = create_app()

app.py为:

...

def create_app():
    """
    app = Flask(__name__)
    ...
    return app

如何从我的app访问wsgi.py模块中创建的tasks.py单例实例?

0 个答案:

没有答案
相关问题