Python
threading
Timer
repeat function
scheduled tasks

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.

python
1import threading
2
3def display_message():
4    print("This function is called every 5 seconds")
5
6# Create a Timer object
7timer = threading.Timer(5.0, display_message)
8
9# Start the Timer thread
10timer.start()

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:

python
1import threading
2
3def repeat_function(interval):
4    print("This function is called every {} seconds".format(interval))
5    # Restart the timer
6    threading.Timer(interval, repeat_function, [interval]).start()
7
8# Call with desired interval
9repeat_function(5.0)

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:

  1. Non-blocking: the main thread keeps running while the timer works in a separate thread.
  2. Simple: periodic tasks without an explicit loop.
  3. Flexible: easy to use for event-driven or scheduled tasks.

Limits:

  1. Precision: not suitable for precise timing. Thread switching and processing overhead can delay execution.
  2. 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

MethodNon-blockingPrecisionUse Case
threading.TimerYesLowSuitable for simple periodic tasks with no high precision requirements
sched.schedulerNoHighTasks requiring precise timing and managed events
asyncioYesHighAsynchronous I/O operations and high-level structured tasks
while loopNoVariableSimple repetition when blocking is not an issue

If timing accuracy matters, sched.scheduler or asyncio may suit you better than a chain of timers.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions