ccproxy.core.async_task_manager¶
ccproxy.core.async_task_manager
¶
Centralized async task management for lifecycle control and resource cleanup.
This module provides a centralized task manager that tracks all spawned async tasks, handles proper cancellation on shutdown, and provides exception handling for background tasks to prevent resource leaks and unhandled exceptions.
TaskInfo
¶
Information about a managed task.
Source code in ccproxy/core/async_task_manager.py
get_exception
¶
Get the exception if the task failed.
Source code in ccproxy/core/async_task_manager.py
AsyncTaskManager
¶
Centralized manager for async tasks with lifecycle control.
This class provides: - Task registration and tracking - Automatic cleanup of completed tasks - Graceful shutdown with cancellation - Exception handling for background tasks - Task monitoring and statistics
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cleanup_interval
|
float
|
Interval for cleaning up completed tasks (seconds) |
30.0
|
shutdown_timeout
|
float
|
Timeout for graceful shutdown (seconds) |
30.0
|
max_tasks
|
int
|
Maximum number of tasks to track (prevents memory leaks) |
1000
|
Source code in ccproxy/core/async_task_manager.py
start
async
¶
Start the task manager and its cleanup task.
Source code in ccproxy/core/async_task_manager.py
stop
async
¶
Stop the task manager and cancel all managed tasks.
Source code in ccproxy/core/async_task_manager.py
create_task
async
¶
Create a managed task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[T]
|
Coroutine to execute |
required |
name
|
str | None
|
Optional name for the task (auto-generated if None) |
None
|
creator
|
str | None
|
Optional creator identifier for debugging |
None
|
cleanup_callback
|
Callable[[], None] | None
|
Optional callback to run when task completes |
None
|
Returns:
| Type | Description |
|---|---|
Task[T]
|
The created task |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If task manager is not started or has too many tasks |
Source code in ccproxy/core/async_task_manager.py
get_task_stats
async
¶
Get statistics about managed tasks.
Source code in ccproxy/core/async_task_manager.py
list_active_tasks
async
¶
Get list of active tasks with details.
Source code in ccproxy/core/async_task_manager.py
create_managed_task
async
¶
create_managed_task(
coro,
*,
name=None,
creator=None,
cleanup_callback=None,
container=None,
task_manager=None,
)
Create a managed task using the dependency-injected task manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[T]
|
Coroutine to execute |
required |
name
|
str | None
|
Optional name for the task |
None
|
creator
|
str | None
|
Optional creator identifier |
None
|
cleanup_callback
|
Callable[[], None] | None
|
Optional cleanup callback |
None
|
container
|
Optional[ServiceContainer]
|
Optional service container for resolving the task manager |
None
|
task_manager
|
Optional[AsyncTaskManager]
|
Optional explicit task manager instance |
None
|
Returns:
| Type | Description |
|---|---|
Task[T]
|
The created managed task |
Source code in ccproxy/core/async_task_manager.py
start_task_manager
async
¶
Start the dependency-injected task manager.
Source code in ccproxy/core/async_task_manager.py
stop_task_manager
async
¶
Stop the dependency-injected task manager.
Source code in ccproxy/core/async_task_manager.py
create_fire_and_forget_task
¶
Create a fire-and-forget managed task from a synchronous context.
This function schedules a coroutine to run as a managed task without needing to await it. Useful for calling from synchronous functions that need to schedule background work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[T]
|
Coroutine to execute |
required |
name
|
str | None
|
Optional name for the task |
None
|
creator
|
str | None
|
Optional creator identifier |
None
|
container
|
Optional[ServiceContainer]
|
Optional service container to resolve the task manager |
None
|
task_manager
|
Optional[AsyncTaskManager]
|
Optional explicit task manager instance |
None
|