Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active February 12, 2025 16:48
Show Gist options
  • Select an option

  • Save twolfson/a1b329e9353f9b575131 to your computer and use it in GitHub Desktop.

Select an option

Save twolfson/a1b329e9353f9b575131 to your computer and use it in GitHub Desktop.

Revisions

  1. twolfson revised this gist Mar 12, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original 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
  2. twolfson revised this gist Feb 4, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original 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()
    db.session.remove()
    # 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)
    ```
  3. twolfson created this gist Feb 4, 2015.
    26 changes: 26 additions & 0 deletions README.md
    Original 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)
    ```