我正在尝试遵循http://flask.pocoo.org/docs/0.12/patterns/celery/中的说明,因此我可以在芹菜任务中执行flask / socketIO操作。我的目录结构有点不同,但我对进口没有任何好运。
我的目录结构如下:
from app import socketio, app
if __name__ == '__main__':
socketio.run(app, debug=True, port=443, ssl_context='adhoc')
我从__init__.py
from flask import Flask, request
from flask_socketio import SocketIO
from .ctasks import subtaskcaller, make_celery
from .helper import wait_to_finish
async_mode = None
app = Flask(__name__)
app.config.from_object('config')
socketio = SocketIO(app, async_mode=async_mode)
cel = make_celery(app)
from .auth import SamlManager
saml_manager = SamlManager()
saml_manager.init_app(app)
from app import views, socks, saml, helper, ctasks
ctasks.py
from celery import Celery
from config import *
from .helper import wait_to_finish, emitter
import time
from app import cel
def make_celery(app):
c = Celery(app.import_name, backend=CELERY_RESULT_BACKEND, broker=CELERY_BROKER_URL)
c.conf.update(app.config)
taskbase = c.Task
class ContextTask(taskbase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return taskbase.__call__(self, *args, **kwargs)
c.Task = ContextTask
return c
@cel.task(name='tasks.tester', serializer='pickle')
def tester():
emitter('emit from subsubtask')
for i in range(1, 50):
time.sleep(1)
print('test {0}'.format(i))
x = True
return x
@cel.task(name='task.subtaskcaller', serializer='pickle')
def subtaskcaller():
emitter('emit from subtask')
finished = tester.delay()
wait_to_finish(finished)
return finished
ctasks.py
ImportError: cannot import name 'cel'
尝试从echo json_encode( array_merge($results, $results2) );
中的应用导入cel时出错:
@transaction.atomic
def create(self, validated_data):
usuario = self.context['request'].user
inmobiliaria = get_object_or_404(Inmobiliaria, user=usuario)
validated_data['inmobiliaria'] = inmobiliaria
答案 0 :(得分:2)
在__init__.py
中,您只有cel
。您的celery
文件中没有名为__init__
的对象,因此无法将其导入其他文件。您可以尝试from app import cel
。˛
编辑:
在__init__
from .ctasks import subtaskcaller, make_celery
中的
但在ctasks
中你从app导入cel
(此时尚不存在,此时只存在Flask,request和SocketIO)。
所以你需要将@cel
装饰的功能放在另一个脚本中,你可以在__init__