The best way is to start the timer thread once. Inside your timer thread you'd code the following

class MyThread(Thread):
    def __init__(self, event):
        Thread.__init__(self)
        self.stopped = event

    def run(self):
        while not self.stopped.wait(0.5):
            print("my thread")
            # call a function

In the code that started the timer, you can then set the stopped event to stop the timer.

stopFlag = Event()
thread = MyThread(stopFlag)
thread.start()
# this will stop the timer
stopFlag.set()
Answer from Hans Then on Stack Overflow
🌐
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.
🌐
Super Fast Python
superfastpython.com β€Ί timer-thread-in-python
Threading Timer Thread in Python – SuperFastPython
March 1, 2022 - Running the example starts the timer thread with the target task() function to trigger in 3 seconds. The main thread waits for a second, then cancels the timer thread, preventing the task() function from executing and the message from being reported. Canceling the timer...
🌐
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.
Call Β  +917738666252
Address Β  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Delft Stack
delftstack.com β€Ί home β€Ί howto β€Ί python β€Ί threading.timer python
Timer Class in the Threading Module in Python | Delft Stack
October 10, 2023 - Now that we are done with the theory, let us understand how we can practically use this class to create an infinite timer. Refer to the following code for the same. from time import sleep from threading import Timer from datetime import datetime class MyInfiniteTimer: """ A Thread that executes infinitely """ def __init__(self, t, hFunction): self.t = t self.hFunction = hFunction self.thread = Timer(self.t, self.handle_function) def handle_function(self): self.hFunction() self.thread = Timer(self.t, self.handle_function) self.thread.start() def start(self): self.thread = Timer(self.t, self.handle_function) self.thread.start() def cancel(self): self.thread.cancel() def print_current_datetime(): print(datetime.today()) t = MyInfiniteTimer(1, print_current_datetime) t.start() sleep(5) t.cancel() sleep(5) t.start() sleep(5) t.cancel()
🌐
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
🌐
ProgramCreek
programcreek.com β€Ί python β€Ί example β€Ί 2317 β€Ί threading.Timer
Python Examples of threading.Timer
def execWait(cmd, outfile=None, timeout=0): result = "" env = os.environ proc = subprocess.Popen(cmd, executable='/bin/bash', env=env, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True) if timeout: timer = threading.Timer(timeout, proc.kill) timer.start() result = proc.communicate()[0] if timeout: if timer.is_alive(): timer.cancel() if outfile: if Utils.fileExists(outfile): print "FILE ALREADY EXISTS!!!!" else: tmp_result = "\033[0;33m(" + time.strftime( "%Y.%m.%d-%H.%M.%S") + ") <pentest> #\033[0m " + cmd + Utils.newLine() + Utils.newLine() + result Utils.writeFile(tmp_result, outfile) return result
🌐
Inductive Automation
forum.inductiveautomation.com β€Ί ignition
Python's Timer: from threading import Timer - Ignition - Inductive Automation Forum
September 20, 2022 - The code is fairly simple: def JustTesting(): from threading import Timer msg = "Starting" system.perspective.print(message = msg, destination="client") def runThisLater(param): msg = "Inside Timer" system.perspective.print(message = msg, ...
Find elsewhere
🌐
Python Assets
pythonassets.com β€Ί posts β€Ί executing-code-every-certain-time
Executing Code Every Certain Time | Python Assets
March 13, 2022 - Following this same principle, we can use the threading.Thread class to move a function into a child thread, where a code execution is repeated every certain time, with the help of the timer.sleep() (docs) standard function.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί timer-objects-python
Timer Objects in Python - GeeksforGeeks
June 28, 2017 - threading.Timer(interval, function, args = None, kwargs = None) Create a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed.
🌐
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.
🌐
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 - The next part contains all the ... (In this example, we’ve used a for loops). Similarly, record the end time of the CPU (Default time) and check the total execution time by using end – start. This will print the time taken by the code in seconds. With the use of this technique, you can check the time taken by your code. The only problem with this method is that the CPU time can be changed while the code is running. This will result in problematic behavior in python timer. time.thread_time() returns ...
🌐
Real Python
realpython.com β€Ί intro-to-python-threading
An Intro to Threading in Python – Real Python
August 5, 2024 - An example would be if you have a pool of connections and want to limit the size of that pool to a specific number. A threading.Timer is a way to schedule a function to be called after a certain amount of time ...
🌐
Javatpoint
javatpoint.com β€Ί python-threading-timer
Python Threading Timer - Javatpoint
August 6, 2023 - Python Threading Timer with Codes in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
🌐
GitHub
gist.github.com β€Ί cypreess β€Ί 5481681
Python periodic thread using timer. You can cancel this thread any time. Thread will live at maximum to the end of one single processing run, otherwise it will end in the same time (especially during a wait time for next run). This code avoids the problem of waiting very long for thread exiting, because it does not uses time.sleep(). Please be aware that this is naive implementation of periodic thread, it does not guarantee runing it __every__ `period` second, it will just wait before following runs at leas
Python periodic thread using timer. You can cancel this thread any time. Thread will live at maximum to the end of one single processing run, otherwise it will end in the same time (especially during a wait time for next run). This code avoids the problem of waiting very long for thread exiting, because it does not uses time.sleep(). Please be aware that this is naive implementation of periodic thread, it does not guarantee runing it __every__ `period` second, it will just wait before following runs at least `period` seconds.
🌐
LabEx
labex.io β€Ί tutorials β€Ί python-easy-to-use-threading-7839
Python Threading Tutorial | Concurrent Execution | LabEx
In Python, you can use threading.Timer object to schedule a function to run after a specific time has passed.
🌐
Educative
educative.io β€Ί answers β€Ί how-to-create-a-timer-in-python
How to create a timer in Python
You can create a countdown timer using the threading module in Python, which allows a function to be executed after a specified number of seconds. This is a basic example of triggering an action after waiting for a few seconds.
🌐
Studytonight
studytonight.com β€Ί python β€Ί python-threading-timer-object
Python Timer Object | Studytonight
Below we have a simple example where we create a timer object and start it. import threading def task(): print("timer object task running...") if __name__=='__main__': t = threading.Timer(10, task) t.start() # after 10 seconds, task will be executed