Scheduling a Repeated Task Every 10 Seconds in Python

  • Share this:

Code introduction


The code defines a function named random_task that prints a message when scheduled to run. It then uses the every() method from the Schedule library to call this function every 10 seconds. Finally, it runs the scheduler in an infinite loop.


Technology Stack : The code defines a function named random_task that prints a message when scheduled to run. It then uses the every() method from the Schedule library to call this function every 10 seconds. Finally, it runs the scheduler in an infinite loop.

Code Type : Timed task scheduling

Code Difficulty : Intermediate


                
                    
import schedule
import time

def random_task():
    print("This is a randomly scheduled task.")

# Schedule the function to run every 10 seconds
schedule.every(10).seconds.do(random_task)

# Run the scheduler
while True:
    schedule.run_pending()
    time.sleep(1)