Skip to main content

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 by wait_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

sample.py

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

  1. 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.
  2. cancel_task(task_name: str)

    • Cancel a task from the queue.
    • Args:
      • task_id (uuid.UUID): The ID of the task to cancel.
  3. wait_for_next_task() -> AsyncGenerator[Tuple[datetime, str], None]

    • Wait for the next task in the queue.
    • Yields the datetime and name of 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

awaitlist-0.1.2.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

awaitlist-0.1.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file awaitlist-0.1.2.tar.gz.

File metadata

  • Download URL: awaitlist-0.1.2.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.8

File hashes

Hashes for awaitlist-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5a4385656fc738326feb05819a9f9dc4ef7dd69b58b0e611d013ef99912171bf
MD5 6423f7ae4fe4738b723a3dfcf75f50ef
BLAKE2b-256 75fb8051dbe1867ec92f24b3f312713bca260a103b222d48e20f9afe3afad2ab

See more details on using hashes here.

File details

Details for the file awaitlist-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: awaitlist-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.8

File hashes

Hashes for awaitlist-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 80da5f36ec13b347764f6a70a548dd9bc463c89a9ecff98583fab7e281c3388c
MD5 9713d2a647a968a06ac103eb83a99355
BLAKE2b-256 0d92ef31415ff78d7c82750ba202f305165d12a5b2cb66d99d597c5ffd6c9e5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page