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