python
44 lines · 6 steps
A self-rescheduling periodic task in Python
A thread-safe helper that runs a function on a repeating interval using chained one-shot timers.
Explained by
highlit
1import threading
2import logging
3
4logger = logging.getLogger(__name__)
5
6
7class PeriodicTask:
8 def __init__(self, interval, func, *args, **kwargs):
9 self.interval = interval
10 self.func = func
11 self.args = args
12 self.kwargs = kwargs
13 self._timer = None
14 self._lock = threading.Lock()
15 self._running = False
16
17 def start(self):
18 with self._lock:
19 if self._running:
20 return
21 self._running = True
22 self._schedule()
23
24 def _schedule(self):
25 self._timer = threading.Timer(self.interval, self._run)
26 self._timer.daemon = True
27 self._timer.start()
28
29 def _run(self):
30 try:
31 self.func(*self.args, **self.kwargs)
32 except Exception:
33 logger.exception("periodic task %r failed", self.func.__name__)
34 finally:
35 with self._lock:
36 if self._running:
37 self._schedule()
38
39 def stop(self):
40 with self._lock:
41 self._running = False
42 if self._timer is not None:
43 self._timer.cancel()
44 self._timer = None
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Chaining one-shot timers gives a repeating schedule that measures the interval between the end of one run and the start of the next.
- 2A lock guarding the running flag prevents start/stop races from leaving stray timers scheduled.
- 3Catching exceptions inside the task keeps one failure from silently killing the whole repeating loop.
Related explainers
python
from functools import lru_cache import math
Memoizing number theory with lru_cache
memoization
number-theory
caching
Intermediate
8 steps
python
import re from collections import defaultdict from pathlib import Path
Summarizing log files by date in Python
regex
parsing
aggregation
Intermediate
7 steps
go
package fetch import ( "context"
Concurrent API fetches with errgroup in Go
concurrency
goroutines
error-handling
Intermediate
8 steps
javascript
export function cloneState(state) { if (typeof structuredClone !== "function") { throw new Error("structuredClone is not available in this runtime"); }
Deep cloning with structuredClone
deep-copy
error-handling
immutability
Intermediate
7 steps
rust
use serde::Deserialize; use serde_json::Value; use std::collections::HashMap;
Modeling nested JSON with serde in Rust
deserialization
json
struct-mapping
Intermediate
9 steps
java
import java.util.concurrent.atomic.AtomicInteger; public final class RequestCounter {
A thread-safe request counter with AtomicInteger
concurrency
atomics
lock-free
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-self-rescheduling-periodic-task-in-python-explained-python-55bf/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.