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.

Functionality

  • Schedule the execution of code at specific times or intervals
  • Schedule repeat function calls at specific time intervals
  • Simple and intuitive interface for scheduling code
  • Lightweight and easy to use
  • No external 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()

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.5.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

sched2-0.5.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file sched2-0.5.0.tar.gz.

File metadata

  • Download URL: sched2-0.5.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for sched2-0.5.0.tar.gz
Algorithm Hash digest
SHA256 b1f786e42303700434c35c2103e673623b7cebce6405bc66be94bb94e9bd94ae
MD5 296350069c6a47575aba0a97fbe49d5c
BLAKE2b-256 9ba3149442fe31b4c711402c97c088572eb15f0e53329a5724c7e9131cdd7a1e

See more details on using hashes here.

File details

Details for the file sched2-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: sched2-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for sched2-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bbfaf75fb4c35de92bd4bbb867c7bb1b91a3e0c476ba532be3a90d27d5f6a2f5
MD5 2617f11649097d729e9ba87b48642578
BLAKE2b-256 c7e9ab05d9f4289caf07feaa108b0c76463c0fbdc20ae690b86a3468b2e48630

See more details on using hashes here.

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