Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sched2-0.6.0.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

sched2-0.6.0-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page