A Python library for scheduling tasks at specific intervals.
In many situations the number of threads that can be used to run tasks is limited. For example, on embbded devices or in a web server. So if we can use only one thread, how can we schedule tasks to run at specific intervals?
The purpose of this library is to provide a lightweight and convenient way to schedule tasks to run at specific intervals. It can be used in various scenarios where you need to automate repetitive tasks or perform actions periodically. And all tasks are run in a single thread.
- Schedule tasks to run at specific intervals.
- Option to run a task immediately upon scheduling.
- Specify the number of times a task should run before stopping.
- Ability to cancel scheduled tasks.
- Lightweight and easy to use.
- Use new thread to run tasks, or run in the current thread.
from heapq_scheduler.heapq_scheduler import Scheduler
def task():
print("Running task...")
scheduler = Scheduler()
scheduler.schedule(task, period=5, immediately=True, life=10)
scheduler.start()
scheduler.join()
How to install and run this project
To install the Scheduler library, run the following command:
pip install heapq_scheduler
The Scheduler library requires Python 3.6 or higher.
To use the Scheduler library in your project, follow these steps:
- Import the
Scheduler
class from thescheduler
module. - Create an instance of the
Scheduler
class. - Define a function that represents the task you want to schedule.
- Use the
schedule
method of theScheduler
instance to schedule the task. - Optionally, start the scheduler using the
start
method. - Optionally, join the scheduler using the
join
method to wait for it to finish.
Here is an example:
from scheduler import Scheduler
def task():
print("Running task...")
scheduler = Scheduler()
scheduler.schedule(task, period=5, immediately=True, life=10)
scheduler.start()
scheduler.join()
To contribute to the development of the Scheduler library, follow these steps:
- Fork the repository.
- Create a new branch.
- Make your changes and commit them.
- Push your changes to your fork.
- Submit a pull request.