Last active
February 12, 2025 16:48
-
-
Save twolfson/a1b329e9353f9b575131 to your computer and use it in GitHub Desktop.
Revisions
-
twolfson revised this gist
Mar 12, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ # Last update: Feb 3, 2015 Flask-SQLAlchemy has some nice built-ins (e.g. accessing `query` directly on classes). To continue leveraging these nicities while still inside of a Celery worker, we need to make sure we setup/teardown in a similar fashion to Flask-SQLAlchemy does on Flask. ## Setup -
twolfson revised this gist
Feb 4, 2015 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,13 +14,17 @@ https://github.com/mitsuhiko/flask-sqlalchemy/blob/2.0/flask_sqlalchemy/__init__ To create a similar experience, we can run teardown when a Celery task ends: > We put in a clause about `CELERY_ALWAYS_EAGER` to prevent conflicts with Flask's normal execution. ```python from celery.signals import task_postrun def handle_celery_postrun(retval=None, *args, **kwargs): """After each Celery task, teardown our db session""" if app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']: if not isinstance(retval, Exception): db.session.commit() # If we aren't in an eager request (i.e. Flask will perform teardown), then teardown if not app.config['CELERY_ALWAYS_EAGER']: db.session.remove() task_postrun.connect(handle_celery_postrun) ``` -
twolfson created this gist
Feb 4, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ Flask-SQLAlchemy has some nice built-ins (e.g. accessing `query` directly on classes). To continue leveraging these nicities while still inside of a Celery worker, we need to make sure we setup/teardown in a similar fashion to Flask-SQLAlchemy does on Flask. ## Setup Flask-SQLAlchemy uses `create_scoped_session` at startup which avoids any setup on a per-request basis. https://github.com/mitsuhiko/flask-sqlalchemy/blob/2.0/flask_sqlalchemy/__init__.py#L668 This means Celery can piggyback off of this initialization. ## Teardown Flask-SQLAlchemy tears down when we leave the request/application context. https://github.com/mitsuhiko/flask-sqlalchemy/blob/2.0/flask_sqlalchemy/__init__.py#L747-L753 To create a similar experience, we can run teardown when a Celery task ends: ```python from celery.signals import task_postrun def handle_celery_postrun(retval=None, *args, **kwargs): """After each Celery task, teardown our db session""" if app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']: if not isinstance(retval, Exception): db.session.commit() db.session.remove() task_postrun.connect(handle_celery_postrun) ```