Keep a Redis lock alive by refreshing its TTL every *interval* seconds. Intended to run as a background task (via ``asyncio.create_task``) alongside a long-running operation. Cancel the task when the operation finishes; the caller is responsible for releasing the lock afterwards.
(lock: Lock, interval: int)
| 34 | |
| 35 | |
| 36 | async def refresh_lock_periodically(lock: Lock, interval: int) -> None: |
| 37 | """Keep a Redis lock alive by refreshing its TTL every *interval* seconds. |
| 38 | |
| 39 | Intended to run as a background task (via ``asyncio.create_task``) alongside |
| 40 | a long-running operation. Cancel the task when the operation finishes; the |
| 41 | caller is responsible for releasing the lock afterwards. |
| 42 | """ |
| 43 | try: |
| 44 | while True: |
| 45 | await asyncio.sleep(interval) |
| 46 | try: |
| 47 | await lock.reacquire() |
| 48 | except LockError: |
| 49 | _logger.warning( |
| 50 | 'redis_lock:periodic_refresh_failed', extra={'key': lock.name} |
| 51 | ) |
| 52 | except asyncio.CancelledError: |
| 53 | pass |