Single-thread interval scheduler
Project description
An elegant way to schedule periodic tasks in Python programs. Both the scheduluer and the task run in the main thread, which avoids the issue of synchronizing the data access between tasks and simplifies exception handling.
Basic example
from ischedule import schedule, run_loop
def task_1():
print("task 1")
def task_2():
print("task 2")
schedule(task_1, interval=1.0)
schedule(task_2, interval=0.2)
run_loop()
Output:
task 2
task 2
task 2
task 2
task 1
task 2
task 2
task 2
task 2
task 2
task 1
Implemnentation details
Periodic scheduling has certain quirks that have been taken care of under the hood by ischedule
. For example, it accounts for the time it takes for the task function to execute. If a task is scheduled every second and takes 0.6 seconds to complete, there will be a delay of only 0.4 seconds between consecutive executions. Delays are not propagated. If the previously-mentioned task is scheduled for execution at t=1 second, but is delayed by 0.3 seconds, the next execution of the same task will never the less be scheduled at t=2 seconds.
What happens during heavy loading
- If more than one task become pending at the same time, they are executed in the order in which they were added to the schedule by
schedule()
. - Regardless of the load, no task will be completely starved. All pending tasks will be executed as soon as possible after they become pending.
- If the execution of a task is delayed that the next execution of the same task become pending, this execution will be skipped.
Exceptions
Exceptions during the execution are propagated out of run_pending()
, and can be dealt with by the caller.
Cancellable loops
If run_loop()
is executed without parameters, it will continue running until the process is terminated. If the program needs to be able to cancel it, it should supply a stop_event
, which is expected to be a threading.Event
. When this event is set, run_loop will cleanly return to the caller after completing the currently pending tasks.
More advanced example
In this example, two jobs are scheduled for periodic execution. The first one is scheduled with an interval of 0.1 seconds, and the second one is scheduled with an interval of 0.5 seconds. The second job takes a lot of time to complete, stress-testing the scheduler.
import time
from ischedule import schedule, run_loop
from threading import Event
start_time = time.time()
stop_event = Event()
def job_1():
dt = time.time() - start_time
print(f"Started a _fast_ job at t={dt:.2f}")
if dt > 3:
stop_event.set()
def job_2():
dt = time.time() - start_time
if dt > 2:
return
print(f"Started a *slow* job at t={dt:.2f}")
time.sleep(1)
schedule(job_1, interval=0.1)
schedule(job_2, interval=0.5)
run_loop(stop_event=stop_event)
print("finished")
Output:
Started a _fast_ job at t=0.10
Started a _fast_ job at t=0.20
Started a _fast_ job at t=0.31
Started a _fast_ job at t=0.41
Started a _fast_ job at t=0.50
Started a *slow* job at t=0.50
Started a _fast_ job at t=1.51
Started a *slow* job at t=1.51
Started a _fast_ job at t=2.52
Started a _fast_ job at t=2.61
Started a _fast_ job at t=2.71
Started a _fast_ job at t=2.81
Started a _fast_ job at t=2.90
Started a _fast_ job at t=3.00
Finished
The fast job runs every 0.1 seconds, and completes quickly. When the slow job starts running at t=0.5, it doesn't return control until one second later, at t=1.50s. By that time, both the fast and the slow job become pending, and are executed in the order they were added to the scheduler. The slow job does not run after t=2.0, so the fast job returns to running normally every 0.1 seconds.
Known issues
None at this time. Issues and suggestions can be submitted to https://github.com/aleksve/ischedule/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
Hashes for ischedule-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0aa5281ad14ebfb3af5b1a8b31a5f27701709ee6453c128d15bb529e82c10be4 |
|
MD5 | 6f84a092873ab73ff80caa89e140bf00 |
|
BLAKE2b-256 | 37021acdda174ee583e4f705500bf272c60d4bd4c2e903d6c458f9b2edcc0e32 |