Event scheduler 2
Project description
Event scheduler 2
sched2' provides a subclass of sched.scheduler with extra functionality.
If you're already using sched then sched2 is a drop-in-place change.
The extra functionality
enter- now also acceptsdatetime.timedeltaobjects as values for the delta parameter.repeat- a new method with the same signature asenterthat re-schedulesactionafter it returns.every- is a decorator variant ofrepeatthat schedules the decorated function at definition time.
Enter
Schedules an action to be called only once after some delay whereas repeat will re-schedule the action to be called again and again forever. It does this by repeatedly pushing the action callable back into the scheduler queue. The delay and priority values are fixed for all reintroductions into the queue.
Every
A decorator that provides a friendly way of scheduling functions at definition time.
Install
pip install sched2
Use
from urllib.request import urlopen
from sched2 import scheduler
sc = scheduler()
# repeatedly print public IP every 60 seconds
@sc.every(60)
def echo_ip():
ip = urlopen("https://icanhazip.com/").read().decode("utf-8").strip()
print(f"ip: {ip}")
sc.run()
Now a less realistic example showing all the extra functionality
from time import time
from datetime import datetime, timedelta
from sched2 import scheduler
started_at = time()
# we'll use this in a bit
def echo_time_elapsed():
seconds_since_started = round(time() - started_at, 2)
print(f"started {seconds_since_started}s ago")
print(f"started at {started_at}")
# create a scheduler object
sc = scheduler()
# schedule calling a function repeatedly
# with a delay of 10 seconds between calls
sc.repeat(delay=10, priority=1, action=echo_time_elapsed)
# schedule a funcion by decorating it
@sc.every(delay=15)
def print_current_time():
iso_dt = datetime.utcnow().isoformat()
print(f"decorated function - {iso_dt}")
# you can also use datetime.timedelta objects
# see: https://docs.python.org/3/library/datetime.html#timedelta-objects
@sc.every(delay=timedelta(minutes=1))
def echo_iso_date_every_minute():
iso_dt = datetime.utcnow().isoformat()
print(f"decorated function with timedelta - {iso_dt}")
# run the scheduler
sc.run()
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sched2-0.4.0.tar.gz.
File metadata
- Download URL: sched2-0.4.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.0 Darwin/21.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aad7fe6f234f6ace6eeae654949a24b88ae9f101dd2b0a423c6c630885147394
|
|
| MD5 |
94b763cda64dc0eaa476dc50242bbfaf
|
|
| BLAKE2b-256 |
bd2abe274bf4cff3055bb0c74d8f7ea825bd1cf2d5e724cbe19d323081d94fe5
|
File details
Details for the file sched2-0.4.0-py3-none-any.whl.
File metadata
- Download URL: sched2-0.4.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.0 Darwin/21.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9df4b0c7b1aabf668696c8366f7935336bc9bd14d4c051e6d7b6902d286af058
|
|
| MD5 |
00a9834d2edc6f24e4387d11dbf9c886
|
|
| BLAKE2b-256 |
16294eee64569989db457405765b1aa5b5cc416b9a42271691d1b36c70e88afb
|