Preventing a periodic Celery task from running twice concurrently

Backend Live Coding November 13, 2026 python

The timeout on the lock is important: if the worker dies, the lock expires by itself and the system doesn't get stuck.

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# ======================================================================
# Q: How do you make sure a periodic task does not run twice at the same time?
#
# The timeout on the lock is important: if the worker dies, the lock expires by
# itself and the system doesn't get stuck.
# ======================================================================

@shared_task
def sync_all_calendars():
    # cache.add = SET NX: succeeds only if the key does not exist
    if not cache.add('lock:sync_all_calendars', 1, timeout=600):
        logger.info('previous run still in progress, skipping')
        return
    try:
        for ids in batched(active_calendar_ids(), 500):
            sync_calendar_batch.delay(list(ids))
    finally:
        cache.delete('lock:sync_all_calendars')