Python Program To Schedule A Job To Run After A Certain Amount Of Time
Chapter:
Python
Last Updated:
10-08-2023 15:45:20 UTC
Program:
/* ............... START ............... */
import time
def scheduled_job():
print("Scheduled job is running!")
def schedule_job_after(seconds):
print(f"Scheduling job to run after {seconds} seconds...")
time.sleep(seconds)
scheduled_job()
# Schedule the job to run after 5 seconds
schedule_job_after(5)
/* ............... END ............... */
Notes:
-
In this example, the scheduled_job function represents the job you want to run after a certain amount of time. The schedule_job_after function takes the number of seconds as an argument and uses the time.sleep function to pause the program for the specified amount of time. After the pause, it calls the scheduled_job function.
- Keep in mind that the time.sleep function might not be very precise in terms of timing due to various factors like system load and other processes running on the computer. If you need more precise timing, you might want to look into more advanced scheduling libraries like schedule or APScheduler.