task_manager

Asyncio task management.

This module provides task management functionality. Includes both abstract base classes and concrete implementations for managing asyncio tasks with comprehensive monitoring and cleanup capabilities.

class pipecat.utils.asyncio.task_manager.TaskManagerParams(loop: AbstractEventLoop)[source]

Bases: object

Configuration parameters for task manager initialization.

Parameters:

loop – The asyncio event loop to use for task management.

loop: AbstractEventLoop
class pipecat.utils.asyncio.task_manager.BaseTaskManager[source]

Bases: ABC

Abstract base class for asyncio task management.

Provides the interface for creating, monitoring, and managing asyncio tasks.

abstractmethod setup(params: TaskManagerParams)[source]

Initialize the task manager with configuration parameters.

Parameters:

params – Configuration parameters for task management.

abstractmethod get_event_loop() AbstractEventLoop[source]

Get the event loop used by this task manager.

Returns:

The asyncio event loop instance.

abstractmethod create_task(coroutine: Coroutine, name: str) Task[source]

Creates and schedules a new asyncio Task that runs the given coroutine.

The task is added to a global set of created tasks.

Parameters:
  • coroutine – The coroutine to be executed within the task.

  • name – The name to assign to the task for identification.

Returns:

The created task object.

abstractmethod async cancel_task(task: Task, timeout: float | None = None)[source]

Cancels the given asyncio Task and awaits its completion with an optional timeout.

This function removes the task from the set of registered tasks upon completion or failure.

Parameters:
  • task – The task to be cancelled.

  • timeout – The optional timeout in seconds to wait for the task to cancel.

abstractmethod current_tasks() Sequence[Task][source]

Returns the list of currently created/registered tasks.

Returns:

Sequence of currently managed asyncio tasks.

class pipecat.utils.asyncio.task_manager.TaskData(task: Task)[source]

Bases: object

Internal data structure for tracking task metadata.

Parameters:

task – The asyncio Task being managed.

task: Task
class pipecat.utils.asyncio.task_manager.TaskManager[source]

Bases: BaseTaskManager

Concrete implementation of BaseTaskManager.

Manages asyncio tasks. Provides comprehensive task lifecycle management including creation, monitoring, cancellation, and cleanup.

__init__() None[source]

Initialize the task manager with empty task registry.

setup(params: TaskManagerParams)[source]

Initialize the task manager with configuration parameters.

Parameters:

params – Configuration parameters for task management.

get_event_loop() AbstractEventLoop[source]

Get the event loop used by this task manager.

Returns:

The asyncio event loop instance.

Raises:

Exception – If the task manager is not properly set up.

create_task(coroutine: Coroutine, name: str) Task[source]

Creates and schedules a new asyncio Task that runs the given coroutine.

The task is added to a global set of created tasks.

Parameters:
  • coroutine – The coroutine to be executed within the task.

  • name – The name to assign to the task for identification.

Returns:

The created task object.

Raises:

Exception – If the task manager is not properly set up.

async cancel_task(task: Task, timeout: float | None = None)[source]

Cancels the given asyncio Task and awaits its completion with an optional timeout.

This function removes the task from the set of registered tasks upon completion or failure.

Parameters:
  • task – The task to be cancelled.

  • timeout – The optional timeout in seconds to wait for the task to cancel.

current_tasks() Sequence[Task][source]

Returns the list of currently created/registered tasks.

Returns:

Sequence of currently managed asyncio tasks.