Python Scheduler
A scheduler in Python is usually used to run code at a particular time or after a specific interval. You can use various libraries to create schedulers, such as schedule
, APScheduler
, or even the built-in sched
module.
Here are examples for each:
Using the sched
module:
import sched
import time
s = sched.scheduler(time.time, time.sleep)
def print_time():
print("Scheduled Event:", time.time())
# Schedule the event for 5 seconds from now
s.enter(5, 1, print_time)
s.run()
Using the schedule
library:
First, install the schedule
library using pip install schedule
.
import schedule
import time
def print_time():
print("Scheduled Event:", time.time())
# Schedule the job to run every 5 seconds
schedule.every(5).seconds.do(print_time)
while True:
schedule.run_pending()
time.sleep(1)
Using the APScheduler
:
First, install the library using pip install apscheduler
.
from apscheduler.schedulers.blocking import BlockingScheduler
def print_time():
print("Scheduled Event:", time.time())
scheduler = BlockingScheduler()
scheduler.add_job(print_time, 'interval', seconds=5)
scheduler.start()
Each of these libraries offers different features and levels of customization, so you may choose based on your requirements. The built-in sched
module is simple and works well for basic scheduling, while the schedule
library provides a more human-friendly interface. APScheduler
provides more advanced features, such as persisting jobs between restarts and distributed scheduling.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks