Event scheduler 2
Project description
The sched2
module provides a simple and intuitive way to schedule the execution of code in Python. For example, it allows you to schedule a function to be called at a specific time or after a particular delay or to schedule a function to be called repeatedly at a specific time interval. This can be useful for automating tasks or scheduling the execution of code without having to write your own scheduling logic. In addition, it is lightweight and easy to use, making it an excellent choice for scheduling the execution of code in Python.
sched2
implements a subclass of the sched.scheduler
class from Python's standard library that adds additional functionality. This means that sched2
includes all of the features and functionality of the sched
module and adds extra methods. As a result, you can use sched2
in place of sched
in your code without any further modifications, and you will have access to the additional features provided by sched2
. These other features include the repeat
method and the every
decorator, which allow you to repeatedly schedule a function to be called at a specific time interval.
Features
- Schedule tasks to run one time or on a recurring basis
- Simple and intuitive interface for scheduling code
- Lightweight and no extra dependencies
Install
To install the sched2
module, you can use pip
, the package installer for Python. Open a terminal and run the following command:
pip install sched2
Examples
The code bellow defines a function that checks if the IP address has changed and prints a message if it has. Then it creates an instance of a scheduler class and uses the repeat
method to schedule the IP check function to run every two minutes. Finally, it starts the scheduler, so the IP check function will run indefinitely.
from urllib.request import urlopen
from sched2 import scheduler
def check_ip():
# Get the public IP address
global current_ip
ip = urlopen("https://icanhazip.com/").read().decode("utf-8").strip()
# Check if the IP address has changed
if ip != current_ip:
current_ip = ip
print(f"IP changed to {ip}")
# Initialize the current_ip variable to None
current_ip = None
# Create a scheduler
sc = scheduler()
# Run the check_ip function every 120 seconds
sc.repeat(120, 1, check_ip)
# Run the scheduler
sc.run()
The following code creates an instance of a scheduler class and decorates a function, so it runs every two minutes. First, the decorated function gets the public IP address and checks if it has changed. If it has, it updates and prints a message. Finally, it starts the scheduler, so the decorated function runs indefinitely.
from urllib.request import urlopen
from sched2 import scheduler
# Create a scheduler
sc = scheduler()
@sc.every(120) # Run every two minutes
def check_ip():
# Get the public IP address and check if it has changed
global current_ip
ip = urlopen("https://icanhazip.com/").read().decode("utf-8").strip()
if ip != current_ip:
current_ip = ip
print(f"IP changed to {ip}")
# Initialize the current_ip variable to None
current_ip = None
# Run the scheduler
sc.run()
Source Code and Issues
Help improve sched2 by reporting any issues or suggestions on our issue tracker at github.com/medecau/sched2/issues.
Get involved with the development, check out the source code at github.com/medecau/sched2.
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.5.1.tar.gz
.
File metadata
- Download URL: sched2-0.5.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/23.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71d03bfc11c25bdbb8901c2c613d2523e3bdf55558441dcb900cd41455578e9c |
|
MD5 | 02b69c4fad5d6da257994edd187edce7 |
|
BLAKE2b-256 | 3a49447c1710f67fadc784ad3e8b24090b893eca2f2c9b058bd2f44301801eaa |
File details
Details for the file sched2-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: sched2-0.5.1-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/23.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1f4f7a1e295552096c51bdee15def0e58464eb0de44be2cb6354aca3b61dad6 |
|
MD5 | 83abe9559c21b0d96392c56dde042723 |
|
BLAKE2b-256 | e4f688ea43340a57b77075bbdcb7b6963f9e6132adfd6355191b8e79da2919d8 |