Precise_scheduler¶
Python job scheduling for humans. Run Python functions (or any other callable) periodically using a friendly syntax.
A simple to use API for scheduling jobs, made for humans.
In-process scheduler for periodic jobs. No extra processes needed!
Very lightweight and no external dependencies.
Excellent test coverage.
Usage¶
$ pip install precise-scheduler
import datetime
import precise_scheduler
import time
import pause
scheduler = precise_scheduler.Scheduler(schedule_base="last_schedule")
#scheduler = precise_scheduler.Scheduler(schedule_base="last_run_start")
#scheduler = precise_scheduler.Scheduler()
def greet(name):
print("Hello", name, datetime.datetime.now())
time.sleep(1)
scheduler.every(2).seconds.do(greet, name="Alice")
scheduler.every(4).seconds.do(greet, name="Bob")
from precise-scheduler import every, repeat
@repeat(scheduler.every(5).seconds, "World")
@repeat(scheduler.every().day, "Mars")
def hello(planet):
print("Hello", planet, datetime.datetime.now())
time.sleep(0.5)
while True:
pause.until(scheduler.get_next_run())
#print(scheduler.get_next_run())
scheduler.run_pending()
# Hello Alice 2023-03-02 12:24:31.000249
# Hello Alice 2023-03-02 12:24:33.000094
# Hello Bob 2023-03-02 12:24:34.001463
# Hello World 2023-03-02 12:24:35.003073
# Hello Alice 2023-03-02 12:24:35.503961
# Hello Alice 2023-03-02 12:24:37.000157
# Hello Bob 2023-03-02 12:24:38.001703
# Hello Alice 2023-03-02 12:24:39.003366
# Hello World 2023-03-02 12:24:40.004778
# Hello Alice 2023-03-02 12:24:41.000172
Backwards compatibility¶
If the you want to use in simillar way as the you can use
import precise_scheduler as schedule
only when you use change schedule base the behaviour changes.
#scheduler = precise_scheduler.Scheduler(schedule_base=”last_schedule”)
#scheduler = precise_scheduler.Scheduler(schedule_base=”last_run_start”)
The precision part is by default and all schedules are truncated to 0 microseconds thus precise regardless of the schedule base
Comparison with schedule¶
This show how the old version (schedule ) drifts from the schedule on each execution and the new version is accurate to the schedule.
The small microseconds shown in time is the time it takes to execute the print statement, call of function and slight difference of time.sleep(0.001) , which is common for both implementations
$ pip install precise-scheduler
$ pip install schedule
import datetime
import precise_scheduler
import time
import schedule
scheduler = precise_scheduler.Scheduler(schedule_base="last_schedule")
schedule_old = schedule.Scheduler()
def greet(name):
print("Hello", name, datetime.datetime.now())
time.sleep(1)
scheduler.every(3).seconds.do(greet, name="precise_scheduler")
schedule_old.every(3).seconds.do(greet, name="schedule")
while True:
time.sleep(0.001)
scheduler.run_pending()
schedule_old.run_pending()
# Hello precise_scheduler 2023-03-08 11:16:42.000479
# Hello schedule 2023-03-08 11:16:43.001039
# Hello precise_scheduler 2023-03-08 11:16:45.000918
# Hello schedule 2023-03-08 11:16:47.002968
# Hello precise_scheduler 2023-03-08 11:16:48.004551
# Hello precise_scheduler 2023-03-08 11:16:51.000129
# Hello schedule 2023-03-08 11:16:52.001413
More Examples
Background¶
This package is a slight improvement of https://github.com/dbader/schedule
The changes are
Previously the calculation of next schedule was based on end of execution. Now you can also select based on start of last execution start or based on schedule (will be same unless you have a on demand execution).
All schedules will be truncated to 0 microseconds.
The code is updated to newer Pep requirements
The reason for starting this package is the above updates are really needed and the package has not being updated for long and is under MIT licence.
For now the documentation remains the same only difference is mentioned below in code example and you can check out examples folder for python files
Documentation¶
precise_scheduler’s documentation lives at precise_scheduler.readthedocs.io. To guarantee a stable execution schedule you need to move long-running jobs off the main-thread (where the scheduler runs). See Parallel execution for a sample implementation.
Read More¶
- Installation
- Examples
- Run a job every x minute
- Use a decorator to schedule a job
- Pass arguments to a job
- Cancel a job
- Run a job once
- Get all jobs
- Cancel all jobs
- Get several jobs, filtered by tags
- Run a job at random intervals
- Run a job until a certain time
- Time until the next execution
- Run all jobs now, regardless of their scheduling
- Run in the background
- Parallel execution
- Timezone & Daylight Saving Time
- Exception Handling
- Logging
- Multiple schedulers
- Frequently Asked Questions
- AttributeError: ‘module’ object has no attribute ‘every’
- ModuleNotFoundError: No module named ‘precise_scheduler’
- ModuleNotFoundError: ModuleNotFoundError: No module named ‘pytz’
- Does precise_scheduler support time zones?
- What if my task throws an exception?
- How can I run a job only once?
- How can I cancel several jobs at once?
- How to execute jobs in parallel?
- How to continuously run the scheduler without blocking the main thread?
- Another question?
- Reference
- Development
Issues¶
If you encounter any problems, please file an issue along with a detailed description. Please also use the search feature in the issue tracker beforehand to avoid creating duplicates. Thank you 😃
Meta¶
Bibin Varghese - @bibinvargheset - bibinvargheset@gmail.com
This package is a based on https://github.com/dbader/schedule
Inspired by Adam Wiggins’ article “Rethinking Cron” and the clockwork Ruby module.
Distributed under the MIT license. See LICENSE.txt for more information.