Background task manager
Background task manager for async operations in PyAgenity.
This module provides BackgroundTaskManager, which tracks and manages asyncio background tasks, ensuring proper cleanup and error logging.
Classes:
Name | Description |
---|---|
BackgroundTaskManager |
Manages asyncio background tasks for agent operations. |
TaskMetadata |
Metadata for tracking background tasks. |
Attributes:
Name | Type | Description |
---|---|---|
logger |
|
Attributes¶
Classes¶
BackgroundTaskManager
¶
Manages asyncio background tasks for agent operations.
Tracks created tasks, ensures cleanup, and logs errors from background execution. Enhanced with cancellation, timeouts, and metadata tracking.
Methods:
Name | Description |
---|---|
__init__ |
Initialize the BackgroundTaskManager. |
cancel_all |
Cancel all tracked background tasks. |
create_task |
Create and track a background asyncio task. |
get_task_count |
Get the number of active background tasks. |
get_task_info |
Get information about all active tasks. |
wait_for_all |
Wait for all tracked background tasks to complete. |
Source code in pyagenity/utils/background_task_manager.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
Functions¶
__init__
¶
__init__()
Initialize the BackgroundTaskManager.
Source code in pyagenity/utils/background_task_manager.py
39 40 41 42 43 44 |
|
cancel_all
async
¶
cancel_all()
Cancel all tracked background tasks.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in pyagenity/utils/background_task_manager.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
create_task
¶
create_task(coro, *, name='background_task', timeout=None, context=None)
Create and track a background asyncio task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Coroutine
|
The coroutine to run in the background. |
required |
|
str
|
Human-readable name for the task. |
'background_task'
|
|
Optional[float]
|
Timeout in seconds for the task. |
None
|
|
Optional[dict]
|
Additional context for logging. |
None
|
Returns:
Type | Description |
---|---|
Task
|
asyncio.Task: The created task. |
Source code in pyagenity/utils/background_task_manager.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
get_task_count
¶
get_task_count()
Get the number of active background tasks.
Source code in pyagenity/utils/background_task_manager.py
198 199 200 |
|
get_task_info
¶
get_task_info()
Get information about all active tasks.
Source code in pyagenity/utils/background_task_manager.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
wait_for_all
async
¶
wait_for_all(timeout=None, return_exceptions=False)
Wait for all tracked background tasks to complete.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
float | None
|
Maximum time to wait in seconds. |
None
|
|
bool
|
If True, exceptions are returned as results instead of raised. |
False
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in pyagenity/utils/background_task_manager.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
TaskMetadata
dataclass
¶
Metadata for tracking background tasks.
Methods:
Name | Description |
---|---|
__init__ |
|
Attributes:
Name | Type | Description |
---|---|---|
context |
dict[str, Any] | None
|
|
created_at |
float
|
|
name |
str
|
|
timeout |
float | None
|
|
Source code in pyagenity/utils/background_task_manager.py
21 22 23 24 25 26 27 28 |
|