Skip to main content

A Python library for managing asynchronous task dependencies.

Project description

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.

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

asyncio_task_manager-1.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

asyncio_task_manager-1.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_task_manager-1.0.0.tar.gz.

File metadata

File hashes

Hashes for asyncio_task_manager-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3b6b908367512423097c95572838f68eab2ffbe80d82504ce04cc43cd162f5f3
MD5 569d30a676c15139bd30ce2faae39f04
BLAKE2b-256 ae0710cd2c2f2c96f0df8524ef1212611d62bbc329a5f67e6ce4e5775c7d271a

See more details on using hashes here.

File details

Details for the file asyncio_task_manager-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_task_manager-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ba4bfe5de48b6b7d6d327e2b3b630e02927153a52a7d5ac654a98c553425213
MD5 f025b6ceea831a9344c732ad697a88dc
BLAKE2b-256 a0e66a3275861414be9885d59de0d003ead6dc329a297501a6e7312f82152713

See more details on using hashes here.

Supported by

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