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.

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.0.tar.gz (9.6 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.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: awaitlist-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.21

File hashes

Hashes for awaitlist-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c857d7928c44f6d2eef44a1bb2a7b0f300fb621db5b4d7340e25b3656af848c2
MD5 21279e417e31015958bd57aa69b0248d
BLAKE2b-256 330d8e811dda67d6c287728e05e5f03a8b6743f75214a48de32c25fa16006f62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: awaitlist-0.1.0-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

Hashes for awaitlist-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3020f2c6b1d687eaad9b23e7212d10586ca6989e4a1e6129eb82734b9c32b05
MD5 a3aca31d1523987dfab3adcfa1ae023e
BLAKE2b-256 d121b24589b3fdae0e0fff84ec2fb612947b312be751040f0dbf2fa083a5e9e5

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