[docs]defpatch_task(self):TaskBase=self.Task_celery=selfclassContextTask(TaskBase):abstract=Truedef__call__(self,*args,**kwargs):ifhasattr(_celery,"app"):with_celery.app.app_context():# No need for db.session.remove() since it is automatically closed# by flask-sqlalchemy when exit the app context createdreturnTaskBase.__call__(self,*args,**kwargs)else:returnTaskBase.__call__(self,*args,**kwargs)self.Task=ContextTask