Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在wsgi应用程序中访问应用程序单例实例_Python_Flask_Celery_Wsgi_Gunicorn - Fatal编程技术网

Python 在wsgi应用程序中访问应用程序单例实例

Python 在wsgi应用程序中访问应用程序单例实例,python,flask,celery,wsgi,gunicorn,Python,Flask,Celery,Wsgi,Gunicorn,我想定义一些芹菜任务。这是我的任务.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):

我想定义一些芹菜任务。这是我的
任务.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.py
为:

...

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

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

从wgsi导入app
Cellery=create\u Cellery\u app(app)
@KylePittman:不起作用,创建循环导入为什么不仅仅调用
create\u app
?@dirn:app已经由wsgi中间件创建,而且导入create\u app会导致循环导入您应该能够重构应用程序以避免循环导入。
...

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