Asyncio

class pipecat.utils.asyncio.TaskManagerParams(loop: asyncio.events.AbstractEventLoop, enable_watchdog_logging: bool = False, watchdog_timeout: float = 5.0)[source]

Bases: object

Parameters:
  • loop (AbstractEventLoop)

  • enable_watchdog_logging (bool)

  • watchdog_timeout (float)

loop: AbstractEventLoop
enable_watchdog_logging: bool = False
watchdog_timeout: float = 5.0
class pipecat.utils.asyncio.BaseTaskManager[source]

Bases: ABC

abstractmethod setup(params)[source]
Parameters:

params (TaskManagerParams)

abstractmethod async cleanup()[source]
abstractmethod get_event_loop()[source]
Return type:

AbstractEventLoop

abstractmethod create_task(coroutine, name, *, enable_watchdog_logging=None, watchdog_timeout=None)[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:
  • loop (asyncio.AbstractEventLoop) – The event loop to use for creating the task.

  • coroutine (Coroutine) – The coroutine to be executed within the task.

  • name (str) – The name to assign to the task for identification.

  • enable_watchdog_logging (bool) – whether this task should log watchdog processing times.

  • watchdog_timeout (float) – watchdog timer timeout for this task.

Returns:

The created task object.

Return type:

asyncio.Task

abstractmethod async wait_for_task(task, timeout=None)[source]

Wait for an asyncio.Task to complete with optional timeout handling.

This function awaits the specified asyncio.Task and handles scenarios for timeouts, cancellations, and other exceptions. It also ensures that the task is removed from the set of registered tasks upon completion or failure.

Parameters:
  • task (asyncio.Task) – The asyncio Task to wait for.

  • timeout (Optional[float], optional) – The maximum number of seconds to wait for the task to complete. If None, waits indefinitely. Defaults to None.

abstractmethod async cancel_task(task, timeout=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 (asyncio.Task) – The task to be cancelled.

  • timeout (Optional[float]) – The optional timeout in seconds to wait for the task to cancel.

abstractmethod current_tasks()[source]

Returns the list of currently created/registered tasks.

Return type:

Sequence[Task]

abstractmethod start_watchdog(task)[source]

Starts the given task watchdog timer. If not reset, a warning will be logged indicating the task is stalling.

Parameters:

task (Task)

abstractmethod reset_watchdog(task)[source]

Resets the given task watchdog timer. If not reset, a warning will be logged indicating the task is stalling.

Parameters:

task (Task)

class pipecat.utils.asyncio.TaskData(task: _asyncio.Task, watchdog_start: asyncio.locks.Event, watchdog_timer: asyncio.locks.Event, enable_watchdog_logging: bool, watchdog_timeout: float)[source]

Bases: object

Parameters:
  • task (Task)

  • watchdog_start (Event)

  • watchdog_timer (Event)

  • enable_watchdog_logging (bool)

  • watchdog_timeout (float)

task: Task
watchdog_start: Event
watchdog_timer: Event
enable_watchdog_logging: bool
watchdog_timeout: float
class pipecat.utils.asyncio.TaskManager[source]

Bases: BaseTaskManager

setup(params)[source]
Parameters:

params (TaskManagerParams)

async cleanup()[source]
get_event_loop()[source]
Return type:

AbstractEventLoop

create_task(coroutine, name, *, enable_watchdog_logging=None, watchdog_timeout=None)[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:
  • loop (asyncio.AbstractEventLoop) – The event loop to use for creating the task.

  • coroutine (Coroutine) – The coroutine to be executed within the task.

  • name (str) – The name to assign to the task for identification.

  • enable_watchdog_logging (bool) – whether this task should log watchdog processing time.

  • watchdog_timeout (float) – watchdog timer timeout for this task.

Returns:

The created task object.

Return type:

asyncio.Task

async wait_for_task(task, timeout=None)[source]

Wait for an asyncio.Task to complete with optional timeout handling.

This function awaits the specified asyncio.Task and handles scenarios for timeouts, cancellations, and other exceptions. It also ensures that the task is removed from the set of registered tasks upon completion or failure.

Parameters:
  • task (asyncio.Task) – The asyncio Task to wait for.

  • timeout (Optional[float], optional) – The maximum number of seconds to wait for the task to complete. If None, waits indefinitely. Defaults to None.

async cancel_task(task, timeout=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 (asyncio.Task) – The task to be cancelled.

  • timeout (Optional[float]) – The optional timeout in seconds to wait for the task to cancel.

current_tasks()[source]

Returns the list of currently created/registered tasks.

Return type:

Sequence[Task]

start_watchdog(task)[source]

Starts the given task watchdog timer. If not reset, a warning will be logged indicating the task is stalling. If the timer was already started a warning will be logged.

Parameters:

task (Task)

reset_watchdog(task)[source]

Resets the given task watchdog timer. If not reset, a warning will be logged indicating the task is stalling.

Parameters:

task (Task)