我用我的SQLAlchemy模型(從sqlalchemy.ext.declarative.declarative_base派生)連同燒瓶SQLAlchemy的推芹菜任務
當我嘗試運行任何芹菜任務(只空)
@celery.task()
def empty_task():
pass
在共同燒瓶視圖
@blueprint.route(...)
def view():
image = Image(...)
db.session.add(image)
db.session.flush()
#this cause later error
empty_task()
#now accessing attributes ends with DetachedInstanceError
return jsonify({'name': image.name, ...}
我得到
DetachedInstanceError: Instance <Image at 0x7f6d67e37b50> is not bound to a Session; attribute refresh operation cannot proceed
當我嘗試訪問任務推後模型。沒有任務,它工作正常。如何解決它?
更新: 芹菜使用該任務庫:
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
try:
return TaskBase.__call__(self, *args, **kwargs)
except Exception:
sentry.captureException()
raise
celery.Task = ContextTask