A simple async task scheduler
Project description
AwaitList
AwaitList is an asynchronous task scheduler for Python. It allows you to manage and wait for tasks based on their scheduled execution times using Python's asyncio framework. It is designed to perform long polling-like behavior locally.
Features
- Time-based Task Scheduling: Add tasks with a specific execution time by
add_task(), and user can wait for the task bywait_for_next_task(). - User-controlled Execution: Tasks are not automatically executed when their scheduled time arrives. Instead, users control the execution by iterating through tasks using
async for, allowing flexibility to integrate the task execution into custom workflows. - Dynamic Task Management: Add tasks dynamically at runtime.
Installation
pip install awaitlist
Usage
Example: Scheduling and Executing Tasks
Here’s a basic example of how to use AwaitList to schedule tasks and wait for their execution times.
Code
import asyncio
from datetime import datetime, timedelta
from awaitlist import AwaitList
async def process_tasks(await_list: AwaitList):
"""Fetch tasks from the list and process them sequentially."""
async for task_time, task_name in await_list.wait_for_next_task():
print(f"[Processor] Executing: {task_name} at {datetime.now()}")
async def add_tasks(await_list: AwaitList):
"""Example of dynamically adding tasks."""
now = datetime.now()
print(f"[AddTasks] Current time: {now}")
# Add a task scheduled 5 second later
task_time_5min = now + timedelta(seconds=5)
await await_list.add_task(task_time_5min, "Task 5 second later")
print(f"[AddTasks] Added: Task 5 second later for {task_time_5min}")
await asyncio.sleep(1) # Wait 1 second
# Add a task scheduled 1 second later
task_time_1sec = now + timedelta(seconds=1)
await await_list.add_task(task_time_1sec, "Task 1 second later")
print(f"[AddTasks] Added: Task 1 second later for {task_time_1sec}")
async def main():
await_list = AwaitList()
await asyncio.gather(process_tasks(await_list), add_tasks(await_list))
# Execute
if __name__ == "__main__":
asyncio.run(main())
Output
[AddTasks] Current time: 2025-01-21 01:52:45.588777
[AddTasks] Added: Task 5 second later for 2025-01-21 01:52:50.588777
[AddTasks] Added: Task 1 second later for 2025-01-21 01:52:46.588777
[Processor] Executing: Task 1 second later at 2025-01-21 01:52:46.591487
[Processor] Executing: Task 5 second later at 2025-01-21 01:52:50.593209
API Reference
AwaitList
Methods
-
add_task(task_time: datetime, task_name: str)- Add a task to the queue.
- Args:
task_time (datetime): The scheduled time for the task.task_name (str): The name of the task.
- Returns:
task_id (uuid.UUID): The ID of the task.
-
cancel_task(task_name: str)- Cancel a task from the queue.
- Args:
task_id (uuid.UUID): The ID of the task to cancel.
-
wait_for_next_task() -> AsyncGenerator[Tuple[datetime, str], None]- Wait for the next task in the queue.
- Yields the
datetimeandnameof the next scheduled task.
Use Cases
- Delayed or deferred task execution where tasks are processed only when the user determines they are ready.
- Event-driven applications with precise timing needs, where tasks are scheduled but not automatically executed until iterated.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please fork the repository and submit a pull request with your changes. For major changes, please open an issue first to discuss what you would like to change.
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 awaitlist-0.1.1.tar.gz.
File metadata
- Download URL: awaitlist-0.1.1.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4f6e9a9bc264a1ebb6c0359dd7b6dc4dc4d5ebc25156087ef661460253c5c1b
|
|
| MD5 |
8606cfb8557124f521e1a450fb4b2287
|
|
| BLAKE2b-256 |
218dd43317b44d76e6273b304c5518b6d2acfa03ea35192bad2e93421f86c17d
|
File details
Details for the file awaitlist-0.1.1-py3-none-any.whl.
File metadata
- Download URL: awaitlist-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d04da2e0d942d4749cbaf4e519e70523b9bd4df0ed300b68d92dd507408c0806
|
|
| MD5 |
8433d4bb9b258a72f252213ea00862c1
|
|
| BLAKE2b-256 |
fce9861939f8400df67e521100b9459c0e567eb29402ee9380f8c27c57e6f845
|