我用我的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