At the end of foo(), create a Timer which calls foo() itself after 10 seconds.
Because, Timer create a new thread to call foo().
You can do other stuff without being blocked.

import time, threading
def foo():
    print(time.ctime())
    threading.Timer(10, foo).start()

foo()

#output:
#Thu Dec 22 14:46:08 2011
#Thu Dec 22 14:46:18 2011
#Thu Dec 22 14:46:28 2011
#Thu Dec 22 14:46:38 2011
Answer from kev on Stack Overflow
🌐
Super Fast Python
superfastpython.com › timer-thread-in-python
Threading Timer Thread in Python – SuperFastPython
March 1, 2022 - You can use a timer thread object in Python via the threading.Timer class.
Top answer
1 of 8
228

At the end of foo(), create a Timer which calls foo() itself after 10 seconds.
Because, Timer create a new thread to call foo().
You can do other stuff without being blocked.

import time, threading
def foo():
    print(time.ctime())
    threading.Timer(10, foo).start()

foo()

#output:
#Thu Dec 22 14:46:08 2011
#Thu Dec 22 14:46:18 2011
#Thu Dec 22 14:46:28 2011
#Thu Dec 22 14:46:38 2011
2 of 8
129

Simply sleeping for 10 seconds or using threading.Timer(10,foo) will result in start time drift. (You may not care about this, or it may be a significant source of problems depending on your exact situation.) There can be two causes for this - inaccuracies in the wake up time of your thread or execution time for your function.

You can see some results at the end of this post, but first an example of how to fix it. You need to track when your function should next be called as opposed to when it actually got called and account for the difference.

Here's a version that drifts slightly:

import datetime, threading

def foo():
    print datetime.datetime.now()
    threading.Timer(1, foo).start()

foo()

Its output looks like this:

2013-08-12 13:05:36.483580
2013-08-12 13:05:37.484931
2013-08-12 13:05:38.485505
2013-08-12 13:05:39.486945
2013-08-12 13:05:40.488386
2013-08-12 13:05:41.489819
2013-08-12 13:05:42.491202
2013-08-12 13:05:43.492486
2013-08-12 13:05:44.493865
2013-08-12 13:05:45.494987
2013-08-12 13:05:46.496479
2013-08-12 13:05:47.497824
2013-08-12 13:05:48.499286
2013-08-12 13:05:49.500232

You can see that the sub-second count is constantly increasing and thus, the start time is "drifting".

This is code that correctly accounts for drift:

import datetime, threading, time

next_call = time.time()

def foo():
  global next_call
  print datetime.datetime.now()
  next_call = next_call+1
  threading.Timer( next_call - time.time(), foo ).start()

foo()

Its output looks like this:

2013-08-12 13:21:45.292565
2013-08-12 13:21:47.293000
2013-08-12 13:21:48.293939
2013-08-12 13:21:49.293327
2013-08-12 13:21:50.293883
2013-08-12 13:21:51.293070
2013-08-12 13:21:52.293393

Here you can see that there is no longer any increase in the sub-second times.

If your events are occurring really frequently you may want to run the timer in a single thread, rather than starting a new thread for each event. While accounting for drift this would look like:

import datetime, threading, time

def foo():
    next_call = time.time()
    while True:
        print datetime.datetime.now()
        next_call = next_call+1;
        time.sleep(next_call - time.time())

timerThread = threading.Thread(target=foo)
timerThread.start()

However your application will not exit normally, you'll need to kill the timer thread. If you want to exit normally when your application is done, without manually killing the thread, you should use

timerThread = threading.Thread(target=foo)
timerThread.daemon = True
timerThread.start()
🌐
Bogotobogo
bogotobogo.com › python › Multithread › python_multithreading_subclassing_Timer_Object.php
Python Multithreading Tutorial: Timer Object - 2020
(MainThread) starting timers... (MainThread) waiting before canceling t2 (MainThread) canceling t2 before cancel t2.is_alive() = True after cancel t2.is_alive() = False (t1 ) thread function running (MainThread) done
🌐
Delft Stack
delftstack.com › home › howto › python › threading.timer python
Timer Class in the Threading Module in Python | Delft Stack
October 10, 2023 - It accepts two arguments, namely, interval and function. interval refers to the number of seconds after which the code should be executed, and function is the call back function that should be called when the required time has elapsed.
🌐
Webscale
section.io › home › blog
How to Perform Threading Timer in Python
June 24, 2025 - Get the latest insights on AI, personalization, infrastructure, and digital commerce from the Webscale team and partners.
Find elsewhere
🌐
Python Pool
pythonpool.com › home › blog › understanding the python timer class with examples
Understanding the Python Timer Class with Examples - Python Pool
May 23, 2021 - If we want to run any function after a certain interval of time, we can use the python timer class. In the args parameter, which is None by default, we can specify the arguments we want to give to the callback method.
🌐
Python Assets
pythonassets.com › posts › executing-code-every-certain-time
Executing Code Every Certain Time | Python Assets
March 13, 2022 - A basic solution for communicating threads is the threading.Event class, which is roughly a safe way to share a boolean between two threads. Thus, it is possible to create a boolean that indicates whether the timer should be executed or not, and that can be read and modified both from the main thread and from within the timer() function, which runs in a child thread.
🌐
ProgramCreek
programcreek.com › python › example › 2317 › threading.Timer
Python Examples of threading.Timer
def intr_handler_th(cls, stop_sig_r, intr_r): logging.info("Starting intr thread") while True: try: rdr, _, _ = select.select([stop_sig_r, intr_r], [], []) except select.error as e: logging.info("Ignoring interrupt in select") if e.args[0] == 4: continue else: logging.info("Abandonning interrupt monitoring due to error") break if cls._intr_r in rdr: logging.info("Interrupt received") os.read(cls._intr_r, 1) cls._lock.acquire() for cluster, indices, eid, ctx in cls._registered_execs: logging.info("Interrupt received, sending kill to %d", eid) cls.kill(cluster, indices, exec_id=eid) timer = threading.Timer(10, ctx.cancel) ctx.add_callback(timer.cancel) timer.start() cls._lock.release() return elif cls._stop_sig_r in rdr: os.read(cls._stop_sig_r, 1) logging.debug("Interrupt handler thread was asked to stop") return ·
🌐
Stack Overflow
stackoverflow.com › questions › 45709165 › using-the-python-threading-timer-to-execute-the-callback-function-and-kill-the-p
multithreading - using the python threading.timer to execute the callback function and kill the previous function - Stack Overflow
August 16, 2017 - Here is the code : import threading import time def fun_01() : timer = threading.Timer(3,call_back) print("start~") timer.start() #If the call_back function is executed after 3
🌐
GeeksforGeeks
geeksforgeeks.org › timer-objects-python
Timer Objects in Python - GeeksforGeeks
June 28, 2017 - # Program to cancel the timer import threading def gfg(): print("GeeksforGeeks\n") timer = threading.Timer(5.0, gfg) timer.start() print("Cancelling timer\n") timer.cancel() print("Exit\n") Output: ... In this article, we will discuss the time module and various functions provided by this module with the help of good examples. As the name suggests Python time module allows to work with time in Python.
🌐
Python
docs.python.org › 3 › library › threading.html
threading — Thread-based parallelism — Python 3.14.4 ...
Timers are started, as with threads, by calling their Timer.start method. The timer can be stopped (before its action has begun) by calling the cancel() method.
🌐
Medium
k3no.medium.com › taming-time-in-python-65a625ade93f
Taming Time in Python. Timers, stopwatches, threading and… | by Keno Leon | Medium
November 10, 2021 - If all you need is to have multiple ... Hello from : TIMER ONE Hello from : TIMER TWOA timer is made by defining the interval in seconds to wait before running the function/callback, the only weird thing is the arguments which need ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python threading timer
Python Threading Timer | Various Examples of Python Threading Timer
March 23, 2023 - It can also be stopped even before it has started execution by utilizing the cancel() function corresponding to that threading.Timer() object. ... Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more. We need to import the python library “threading” to use the “Timer” function specified in it.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai