Skip to content

Instantly share code, notes, and snippets.

@Geekfish
Created October 31, 2017 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Geekfish/b853e3b58cd4659bdb3560f04efb0e31 to your computer and use it in GitHub Desktop.
Save Geekfish/b853e3b58cd4659bdb3560f04efb0e31 to your computer and use it in GitHub Desktop.
Transaction Aware Celery Task - SQLAlchemy
# Based on Django version found here:
# https://gist.github.com/tapanpandita/a84938354d32b0f12423d69259e06c2c
from celery import Task
from sqlalchemy import event
from some_module import DBSession
class TransactionAwareTask(Task):
"""
Use this task as a base task if you want to guarantee that a task runs only AFTER a transaction is
successfully committed.
This will help avoid race conditions (when the task runs before a transaction finishes).
"""
abstract = True
def apply_async(self, *args, **kwargs):
"""
NB: This task doesn't return AsyncResult like its parent normally does.
"""
event.listens_for(DBSession, 'after_commit')(
lambda _session: super(TransactionAwareTask, self).apply_async(*args, **kwargs)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment