Created
October 31, 2017 15:20
-
-
Save Geekfish/b853e3b58cd4659bdb3560f04efb0e31 to your computer and use it in GitHub Desktop.
Transaction Aware Celery Task - SQLAlchemy
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 characters
# 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