Python threading.Timer: repeat a function every n seconds
Codemia Knowledge Hub ·
A threading.Timer runs a function once, after a delay, in a separate thread. To repeat a function every n seconds, have the function start a new Timer for itself each time it runs.
How threading.Timer works
threading.Timer is a subclass of Thread that runs a function after a given interval. Because it runs in its own thread, it suits delayed or periodic work that should not block the main thread.
Here display_message runs once, 5 seconds after timer.start().
Repeating every n seconds
To keep it going, restart the timer at the end of each call:
The Timer arguments are:
- Interval: seconds to wait before the call.
- Function: the function to run.
- Arguments: the arguments the function needs, here
[interval].
Each call creates a new timer that starts immediately and schedules the next call.
Strengths and limits of this approach
Strengths:
- Non-blocking: the main thread keeps running while the timer works in a separate thread.
- Simple: periodic tasks without an explicit loop.
- Flexible: easy to use for event-driven or scheduled tasks.
Limits:
- Precision: not suitable for precise timing. Thread switching and processing overhead can delay execution.
- Resources: it can cause resource consumption problems if not managed properly, especially with short intervals.
Typical uses are periodic data fetching from an API or sensor, refreshing UI components without freezing, and scheduled monitoring of system resources or file changes.
Alternatives for periodic execution
| Method | Non-blocking | Precision | Use Case |
threading.Timer | Yes | Low | Suitable for simple periodic tasks with no high precision requirements |
sched.scheduler | No | High | Tasks requiring precise timing and managed events |
asyncio | Yes | High | Asynchronous I/O operations and high-level structured tasks |
while loop | No | Variable | Simple repetition when blocking is not an issue |
If timing accuracy matters, sched.scheduler or asyncio may suit you better than a chain of timers.
Related reading
- ThreadPoolExecutor Block When its Queue Is Full?
- ThreadPoolExecutor with corePoolSize 0 should not execute tasks until task queue is full
- ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew
- Threads and Reactive Programming in Ballerina
- Threads is not executing in parallel python with ThreadPoolExecutor
- Threads vs Asynchronous Networking Twisted Python
- Threads configuration based on no. of CPU-cores
- Threads vs. Async
System design interview problems on this topic
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough
- Design Ticketmaster: solution and walkthrough
- Design a Flash Sale System: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.