How to Execute a Function Every x Seconds in Python

Let’s say you want to run a Python script python every 10 seconds. You can stay there and execute the function manually every 10 seconds later via the terminal or the editor button, or you can use read this article and learn how to schedule the execution of your function to run automatically.

To Execute a function every x seconds in Python:

  1. Use the time.sleep() method
  2. Use the sched module
  3. Use the apscheduler module
  4. Use the threading module

1. time.sleep() – Run Function Every x Seconds

The most common way to execute a function every x seconds in Python is to use the time.sleep() method. The time.sleep() function is a Python built-in function that is used to make the program sleep for the time specified.

We can use the time.sleep() to execute every x seconds. There are two different ways of using time.sleep():

1.1 The wrong way to use time.sleep():

The below code doesn’t execute every 60 seconds. it puts a 60-second gap between executions. It only happens every 60 seconds if your executed code takes no time.

import time
while True:
    # Code executed here

1.2 The Good way to use the time.sleep()

Now look at the below code, this is the best method to execute a Python code every x Seconds. It takes care of the time taken by the code execution. So this is a strongly recommended method if you are using time.sleep() method for the execution of code repeatedly.

import time
starttime = time.time()
while True:
    # Remove the Time taken by code to execute
    time.sleep(60.0 - ((time.time() - starttime) % 60.0))

2. sched module to execute Python code after interval

We can use the sched module to call the function or a block of python code after some interval using the sched module. The sched module is a python module that is used for scheduling the code.

import sched, time
s = sched.scheduler(time.time, time.sleep)
def do_something(sc): 
    print("Doing stuff...")
    # do your stuff
    sc.enter(60, 1, do_something, (sc,))

s.enter(60, 1, do_something, (s,))

3. apscheduler module in Python to execute the function every x seconds

You can use this third party apscheduler module in python. It is very helpful in other ways as well. You can provide other functions like blockscheduler, TwistedScheduler and many more.

from apscheduler.schedulers.background import BlockingScheduler
def print_t():

sched = BlockingScheduler()
sched.add_job(print_t, 'interval', seconds =60) #will do the print_t work for every 60 seconds


4. Use threading module to execute a function after an interval

Here is a python code, that shows you how to use the threading module to execute a function after some interval. You can check it out.

import threading 
import time

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
    self.next_call = time.time()

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

  def start(self):
    if not self.is_running:
      self.next_call += self.interval
      self._timer = threading.Timer(self.next_call - time.time(), self._run)
      self.is_running = True

  def stop(self):
    self.is_running = False

Leave a Comment

Scroll to Top