<s> from celery . task import task as base_task , Task <EOL> import djcelery_transactions . transaction_signals <EOL> from django . db import transaction <EOL> from functools import partial <EOL> import threading <EOL> _thread_data = threading . local ( ) <EOL> def _get_task_queue ( ) : <EOL> """<STR_LIT>""" <EOL> return _thread_data . __dict__ . setdefault ( "<STR_LIT>" , [ ] ) <EOL> class PostTransactionTask ( Task ) : <EOL> """<STR_LIT>""" <EOL> abstract = True <EOL> @ classmethod <EOL> def original_apply_async ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( PostTransactionTask , cls ) . apply_async ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def apply_async ( cls , * args , ** kwargs ) : <EOL> if transaction . is_managed ( ) : <EOL> if not transaction . is_dirty ( ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> transaction . set_dirty ( using = kwargs [ '<STR_LIT>' ] ) <EOL> else : <EOL> transaction . set_dirty ( ) <EOL> _get_task_queue ( ) . append ( ( cls , args , kwargs ) ) <EOL> else : <EOL> return cls . original_apply_async ( * args , ** kwargs ) <EOL> def _discard_tasks ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> _get_task_queue ( ) [ : ] = [ ] <EOL> def _send_tasks ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> queue = _get_task_queue ( ) <EOL> while queue : <EOL> cls , args , kwargs = queue . pop ( <NUM_LIT:0> ) <EOL> cls . original_apply_async ( * args , ** kwargs ) <EOL> task = partial ( base_task , base = PostTransactionTask ) <EOL> transaction . signals . post_commit . connect ( _send_tasks ) <EOL> transaction . signals . post_rollback . connect ( _discard_tasks ) <EOL> transaction . signals . post_transaction_management . connect ( _send_tasks ) </s>
