Event scheduler 2
Project description
The sched2
module extends the general purpose event scheduler sched
from Python's standard library. sched2.scheduler
is a subclass of sched.scheduler
that adds new features such as the every
and cron
decorators. It's a practical tool for automating tasks that need to run repeatedly after certain time delays or at specific times.
Install
sched2
is available on PyPI.
pip install sched2
Examples
The code below uses the every
decorator to schedule checking the public IP address every two minutes.
Then every day at 9:00, the cron
decorator is used to send a report via email.
Finally, the on
decorator is used to send an email when the IP address changes.
from smtplib import SMTP_SSL
from urllib.request import urlopen
from sched2 import scheduler
# Create a scheduler
sc = scheduler()
# we'll use this to remember the last IP address between runs
last_ip = None
@sc.every(30) # Run every two minutes
def print_ip_address():
global last_ip
ip = urlopen("https://icanhazip.com/").read().decode("utf-8").strip()
print(f"Public IP address: {ip}")
last_ip = last_ip or ip # reset last_ip
if ip != last_ip:
last_ip = ip
# Emit an event when the IP address changes
sc.emit("ip_changed", kwargs={"new_ip": ip})
@sc.cron("0 9 * * 1-5") # Run every weekday at 9:00
def send_report():
sendmail("Daily Report", "The numbers are up!")
@sc.on("ip_changed") # Run when 'ip_changed' event is emitted
def send_email(new_ip):
sendmail("IP Address Changed", f"New IP address: {ip}")
def sendmail(subject, body):
"""Send an email using SMTP_SSL."""
with SMTP_SSL("smtp.example.com") as smtp:
smtp.login("me@example.com", "password")
smtp.sendmail(
"me@example.com",
"team@example.com",
f"{subject}\n\n{body}",
)
# Run the scheduler
sc.run()
Source Code and Issues
The source code for sched2
is available on GitHub at github.com/medecau/sched2.
You can help improve sched2 by reporting any issues or suggestions on the issue tracker at github.com/medecau/sched2/issues.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sched2-0.8.2a0.tar.gz
.
File metadata
- Download URL: sched2-0.8.2a0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ed4d500b42d8a8208445e15bccd9f359544c828aa9bfa602ed3c35c04044afd |
|
MD5 | 1b8f719772cb5754c98962dddb4d16e6 |
|
BLAKE2b-256 | d766a990d4031e96be4f203430145f1ec09f79ca194593a9ab6bdd791b390270 |
File details
Details for the file sched2-0.8.2a0-py3-none-any.whl
.
File metadata
- Download URL: sched2-0.8.2a0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34c1ccee179c22cea695c8f1eb2e70efbdd7f54648252543876a07454a12d88a |
|
MD5 | 6cdc956dd15eed0588e11cb509f8e8c2 |
|
BLAKE2b-256 | 6f31716f6dd5c129985f5f87db2509a007c4b080176849961b70e630cd0a75b3 |