How to use Schedule in Python
• 1 minThe schedule
library is a pure Python library that provides a simple way to schedule tasks to be executed periodically. To use schedule
in Python, you need to install the library first by running pip install schedule
in the terminal.
Here's a basic example of how to use schedule
:
import schedule
import time
def job():
print("Job is running...")
schedule.every(10).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
In the code above, the job
function is defined and scheduled to run every 10 seconds using the schedule.every(10).seconds.do(job)
line. The schedule.run_pending()
line is used to run the jobs that are due.
The time.sleep(1)
line is used to give the CPU a break so that it doesn't run the while loop at full speed.
Note that schedule
is a relatively simple library, and it doesn't persist the jobs across program restarts.
If you need more advanced scheduling features, you might want to consider using a more comprehensive library such as APScheduler
,Celery
or Airflow
.