Run certain code every n seconds


Is there a way to, for example, print Hello World! every n seconds?
For example, the program would go through whatever code I had, then once it had been 5 seconds (with time.sleep()) it would execute that code. I would be using this to update a file though, not print Hello World.

For example:

startrepeat("print('Hello World')", .01) # Repeats print('Hello World') ever .01 seconds

for i in range(5):

>> Hello World!
>> 0
>> 1
>> 2
>> Hello World!
>> 3
>> Hello World!
>> 4
Asked By: John Howard



def update():
    import time
    while True:
        print 'Hello World!'

That’ll run as a function. The while True: makes it run forever. You can always take it out of the function if you need.

Answered By: avacariu

You can start a separate thread whose sole duty is to count for 5 seconds, update the file, repeat. You wouldn’t want this separate thread to interfere with your main thread.

Answered By: Kit
import threading

def printit():
  threading.Timer(5.0, printit).start()
  print "Hello, World!"


# continue with the rest of your code

Answered By: Alex Martelli

My humble take on the subject, a generalization of Alex Martelli’s answer, with start() and stop() control:

from threading import Timer

class RepeatedTimer(object):
    def __init__(self, interval, function, *args, **kwargs):
        self._timer     = None
        self.interval   = interval
        self.function   = function
        self.args       = args
        self.kwargs     = kwargs
        self.is_running = False

    def _run(self):
        self.is_running = False
        self.function(*self.args, **self.kwargs)

    def start(self):
        if not self.is_running:
            self._timer = Timer(self.interval, self._run)
            self.is_running = True

    def stop(self):
        self.is_running = False


from time import sleep

def hello(name):
    print "Hello %s!" % name

print "starting..."
rt = RepeatedTimer(1, hello, "World") # it auto-starts, no need of rt.start()
    sleep(5) # your long-running job goes here...
    rt.stop() # better in a try/finally block to make sure the program ends!


  • Standard library only, no external dependencies
  • start() and stop() are safe to call multiple times even if the timer has already started/stopped
  • function to be called can have positional and named arguments
  • You can change interval anytime, it will be effective after next run. Same for args, kwargs and even function!
Answered By: MestreLion

Save yourself a schizophrenic episode and use the Advanced Python scheduler:

The code is so simple:

from apscheduler.scheduler import Scheduler

sched = Scheduler()

def some_job():
    print "Every 10 seconds"

sched.add_interval_job(some_job, seconds = 10)

Answered By: Yan King Yin

Here’s a version that doesn’t create a new thread every n seconds:

from threading import Event, Thread

def call_repeatedly(interval, func, *args):
    stopped = Event()
    def loop():
        while not stopped.wait(interval): # the first call is in `interval` secs
    return stopped.set

The event is used to stop the repetitions:

cancel_future_calls = call_repeatedly(5, print, "Hello, World")
# do something else here...
cancel_future_calls() # stop future calls

See Improve current implementation of a setInterval python

Answered By: jfs

Here is a simple example compatible with APScheduler 3.00+:

# note that there are many other schedulers available
from apscheduler.schedulers.background import BackgroundScheduler

sched = BackgroundScheduler()

def some_job():
    print('Every 10 seconds')

# seconds can be replaced with minutes, hours, or days
sched.add_job(some_job, 'interval', seconds=10)



Alternatively, you can use the following. Unlike many of the alternatives, this timer will execute the desired code every n seconds exactly (irrespective of the time it takes for the code to execute). So this is a great option if you cannot afford any drift.

import time
from threading import Event, Thread

class RepeatedTimer:

    """Repeat `function` every `interval` seconds."""

    def __init__(self, interval, function, *args, **kwargs):
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.start = time.time()
        self.event = Event()
        self.thread = Thread(target=self._target)

    def _target(self):
        while not self.event.wait(self._time):
            self.function(*self.args, **self.kwargs)

    def _time(self):
        return self.interval - ((time.time() - self.start) % self.interval)

    def stop(self):

# start timer
timer = RepeatedTimer(10, print, 'Hello world')

# stop timer
Answered By: Six
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.