Python Scheduler
A Python library supporting task scheduling.
Installation
Install the package using pip:
pip install python-cli-scheduler
Schedule Format
The scheduler format defines how tasks are executed. Use:
^<run_now>@<interval>/<delay>$<end_timestamp>#<retry>
Parameters
| Parameter | Description | Default |
|---|---|---|
run_now |
Whether to execute immediately (True or False). |
True |
interval |
Number of seconds to repeat the task. If None, it runs only once. |
None |
delay |
Seconds to wait in each interval before executing. | 0 |
end_timestamp |
UNIX timestamp to stop execution. If None, runs infinitely. |
None |
retry |
Retry execution if an exception occurs (True or False). |
True |
Usage
Use a Decorator
You can use the scheduler_handle decorator to execute recurring tasks. Below is an example that prints a random "lucky number" every 10 seconds, starting at the 3rd second, and continues for 30 seconds.
import random
import time
from cli_scheduler import scheduler_handle
end_time = int(time.time()) + 30
@scheduler_handle(schedule=f'^true@10/3${end_time}#true')
def rotation_lucky(start=0, end=1000):
n = random.randint(start, end)
print(f'The lucky number is {n}')
if __name__ == '__main__':
rotation_lucky(10, end=100)
Using a Scheduler Job Class
With SchedulerJob, you gain more control over the task execution lifecycle. This example runs a job every 60 seconds, starting at the 3rd second, for 5 minutes. It logs the task lifecycle phases and prints a "lucky number."
import time
import random
from cli_scheduler import SchedulerJob
class RotationLuckJob(SchedulerJob):
def __init__(self, scheduler):
super().__init__(scheduler=scheduler)
def _pre_start(self):
self.logger.info('Rotation luck. Have fun !')
def _start(self):
self.n = random.randint(0, 1000)
def _execute(self, *args, **kwargs):
self.logger.info(f'The lucky number is {self.n}')
def _end(self):
del self.n
def _follow_end(self):
self.logger.info('Done')
if __name__ == '__main__':
end_time = int(time.time()) + 5 * 60
# ^false: Not execute now
# @60: Repeat execute every 60 second
# /3: Execute at second 3
# ${end_time}: Run until end_time
# #false: Don't retry if exception when execute
job = RotationLuckJob(scheduler=f'^false@60/3${end_time}#false')
job.run()
Features
-
Task Scheduling: Automates task execution using a flexible crontab-like syntax.
-
Decorator Support: Simplifies task scheduling using Python function decorators.
-
Lifecycle Hooks: Gain fine-grained control with
SchedulerJobmethods:_pre_start: Preparation before starting the task._start: Runs once before task execution begins._execute: The core task logic is implemented here._end: Final cleanup after task execution ends._follow_end: Actions to follow the end of the task lifecycle.
-
Retry Support: Retry tasks in case of runtime exceptions.
License
This library is licensed under the MIT License. See the LICENSE file for more details.
Contributing
Contributions are welcome! Feel free to submit issues, feature requests, or pull requests to improve the library.
Metadata
Release files for python-cli-scheduler 1.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_cli_scheduler-1.1.7.tar.gz | 9.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_cli_scheduler-1.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.7 kB
Release files / python_cli_scheduler-1.1.7.tar.gz
| Download URL | python_cli_scheduler-1.1.7.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64293447719552d5796e6123aa7e143cea7ad484383c6ae97ea847b6d3534697
|
|
BLAKE2b-256 checksum How to use checksums |
bc6d58923625b0c77e15639788ee4c5b6669cb0e6f3d88f65d2928f15f4d0266
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.9
|
Release files / python_cli_scheduler-1.1.7-py3-none-any.whl
| Download URL | python_cli_scheduler-1.1.7-py3-none-any.whl |
|---|---|
| Size | 12.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
89f417fcb19d395d667a7cb957c715ac355cf39f8804761eda453df2a2372040
|
|
BLAKE2b-256 checksum How to use checksums |
1f9c2ec2800d5feafb204a204be54bb1d7ea3b196e024a9b15622609c244585e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.9
|