latest.ipv8.taskmanager

Module Contents

Classes

TaskManager

Provides a set of tools to maintain a list of asyncio Tasks that are to be

Functions

task(→ Callable)

Register a TaskManager function as an anonymous task and return the Task

latest.ipv8.taskmanager.task(func: Callable) Callable

Register a TaskManager function as an anonymous task and return the Task object so that it can be awaited if needed. Any exceptions will be logged. Note that if awaited, exceptions will still need to be handled.

class latest.ipv8.taskmanager.TaskManager

Provides a set of tools to maintain a list of asyncio Tasks that are to be executed during the lifetime of an arbitrary object, usually getting killed with it.

_check_tasks() None
replace_task(name: Hashable, *args: Any, **kwargs) asyncio.Future

Replace named task with the new one, cancelling the old one in the process.

register_task(name: Hashable, task: Callable | Coroutine | asyncio.Future, *args: Any, delay: float | None = None, interval: float | None = None, ignore: Sequence[type | BaseException] = ()) asyncio.Future

Register a Task/(coroutine)function so it can be canceled at shutdown time or by name.

register_anonymous_task(basename: str, task: Callable | Coroutine | asyncio.Future, *args: Any, **kwargs) asyncio.Future

Wrapper for register_task to derive a unique name from the basename.

register_executor_task(name: str, func: Callable, *args: Any, executor: concurrent.futures.ThreadPoolExecutor | None = None, anon: bool = False, **kwargs) asyncio.Future

Run a synchronous function on the Asyncio threadpool. This function does not work with async functions.

cancel_pending_task(name: Hashable) asyncio.Future

Cancels the named task.

cancel_all_pending_tasks() list[asyncio.Future]

Cancels all the registered tasks. This usually should be called when stopping or destroying the object so no tasks are left floating around.

is_pending_task_active(name: Hashable) bool

Return a boolean determining if a task is active.

get_task(name: Hashable) asyncio.Future | None

Return a task if it exists. Otherwise, return None.

get_tasks() list[asyncio.Future]

Returns a list of all registered tasks, excluding tasks the are created by the TaskManager itself.

get_anonymous_tasks(base_name: str) list[asyncio.Future]

Return all tasks with a given base name.

Note that this method will return ALL tasks that start with the given base name, including non-anonymous ones.

async wait_for_tasks() None

Waits until all registered tasks are done.

async shutdown_task_manager() None

Clear the task manager, cancel all pending tasks and disallow new tasks being added.