python - Increment a counter and trigger an action when a threshold is exceeded -
i have model this class thingy(models.model): # ... failures_count = models.integerfield() i have concurrent processes (celery tasks) need this: do kind of processing if processing fails increment failures_counter of respective thingy if failures_counter exceeds threshold thingy , issue warning, 1 warning. i have ideas how without race condition, example using explicit locks (via select_for_update ): @transaction.commit_on_success def report_failure(thingy_id): current, = (thingy.objects .select_for_update() .filter(id=thingy_id) .values_list('failures_count'))[0] if current == threshold: issue_warning_for(thingy_id) thingy.objects.filter(id=thingy_id).update( failures_count=f('failures_count') + 1 ) or using redis (it's there) synchronization: @transaction.commit_on_success def report_failure(thingy_id): thingy.objects.filter(id=thingy_id).update( ...