Skip to main content

asyncio-task-manager

PyPI - Version PyPI - Python Version

asyncio-task-manager is a Python library that provides an easy-to-use interface for managing asyncio tasks with dependencies. It allows you to create tasks, specify their dependencies, and run them concurrently in a thread pool.


Table of Contents

Features

  • Create tasks with unique task IDs and coroutine functions
  • Specify dependencies between tasks
  • Run tasks concurrently, respecting a maximum thread count
  • Ensure tasks are run only after all their dependencies have finished running

Installation

asyncio-task-manager is available on PyPi:

pip install asyncio-task-manager

Usage

TaskManager(max_threads: int)

Creates a new task manager instance with the specified maximum thread count.

create_task(task_id: str, coroutine: Callable[..., Coroutine], *args, **kwargs) -> None

Creates a new task with the given task ID and coroutine function.

add_dependency(task_id: str, dependency_id: str) -> None

Adds a dependency between two tasks, indicating that task_id depends on the completion of dependency_id.

run_tasks() -> None

Runs all tasks in the dependency graph, respecting the maximum thread count.

Example

Here's a basic example. We create three tasks (task1, task2, and task3) and specify that task2 and task3 depend on task1. We set the maximum thread count to 2. The task manager ensures that task1 is run first, and then task2 and task3 are run concurrently.:

from asyncio_task_manager import TaskManager
import asyncio

async def task1():
    print("Running task 1")
    await asyncio.sleep(1)
    print("Task 1 completed")

async def task2():
    print("Running task 2")
    await asyncio.sleep(2)
    print("Task 2 completed")

async def task3():
    print("Running task 3")
    await asyncio.sleep(1)
    print("Task 3 completed")

async def main():
    task_manager = TaskManager(max_threads=2)

    task_manager.create_task("task1", task1)
    task_manager.create_task("task2", task2)
    task_manager.create_task("task3", task3)

    task_manager.add_dependency("task2", "task1")
    task_manager.add_dependency("task3", "task1")

    await task_manager.run_tasks()

asyncio.run(main())

License

asyncio-task-manager is distributed under the terms of the MIT license.

Release files for asyncio-task-manager 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncio-task-manager 1.0.0
File Size Uploaded
asyncio_task_manager-1.0.0.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncio-task-manager 1.0.0
File Interpreter ABI Platform
asyncio_task_manager-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size:8.2 kB

Release files / asyncio_task_manager-1.0.0.tar.gz

Download URL asyncio_task_manager-1.0.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3b6b908367512423097c95572838f68eab2ffbe80d82504ce04cc43cd162f5f3
BLAKE2b-256 checksum
How to use checksums
ae0710cd2c2f2c96f0df8524ef1212611d62bbc329a5f67e6ce4e5775c7d271a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.26.0

Release files / asyncio_task_manager-1.0.0-py3-none-any.whl

Download URL asyncio_task_manager-1.0.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ba4bfe5de48b6b7d6d327e2b3b630e02927153a52a7d5ac654a98c553425213
BLAKE2b-256 checksum
How to use checksums
a0e66a3275861414be9885d59de0d003ead6dc329a297501a6e7312f82152713
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.26.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page