Wait for a task to complete. Args: task_id: The ID of the task to wait for. polling_interval: The interval in seconds to poll the task status. timeout: The maximum time in seconds to wait for the task to complete. If None, wai
(
self,
task_id: str,
polling_interval: float = 2.0,
timeout: Optional[float] = None,
verbose: bool = False,
use_v3: bool = False,
)
| 229 | return Balance.from_dict(response["data"]) |
| 230 | |
| 231 | async def wait_for_task( |
| 232 | self, |
| 233 | task_id: str, |
| 234 | polling_interval: float = 2.0, |
| 235 | timeout: Optional[float] = None, |
| 236 | verbose: bool = False, |
| 237 | use_v3: bool = False, |
| 238 | ) -> Task: |
| 239 | """ |
| 240 | Wait for a task to complete. |
| 241 | |
| 242 | Args: |
| 243 | task_id: The ID of the task to wait for. |
| 244 | polling_interval: The interval in seconds to poll the task status. |
| 245 | timeout: The maximum time in seconds to wait for the task to complete. |
| 246 | If None, wait indefinitely. |
| 247 | verbose: Print progress updates while polling. |
| 248 | use_v3: When True, poll apiv3 GET /v3/tasks/{id} (required for segment v2 tasks). |
| 249 | |
| 250 | Returns: |
| 251 | The task data. |
| 252 | |
| 253 | Raises: |
| 254 | TripoRequestError: If the request fails. |
| 255 | TripoAPIError: If the API returns an error. |
| 256 | asyncio.TimeoutError: If the task does not complete within the timeout. |
| 257 | """ |
| 258 | start_time = time.monotonic() |
| 259 | |
| 260 | while True: |
| 261 | # Check if we've exceeded the timeout |
| 262 | if timeout is not None: |
| 263 | elapsed = time.monotonic() - start_time |
| 264 | if elapsed >= timeout: |
| 265 | raise asyncio.TimeoutError(f"Task {task_id} did not complete within {timeout} seconds") |
| 266 | |
| 267 | # Get the task status |
| 268 | task = await self.get_task(task_id, use_v3=use_v3) |
| 269 | |
| 270 | # If the task is done, return it |
| 271 | if task.status in (TaskStatus.SUCCESS, TaskStatus.FAILED, TaskStatus.CANCELLED, TaskStatus.BANNED, TaskStatus.EXPIRED): |
| 272 | if verbose: |
| 273 | elapsed = time.monotonic() - start_time |
| 274 | print(f"\nTask {task_id} {task.status} in {elapsed} seconds") |
| 275 | return task |
| 276 | |
| 277 | if verbose: |
| 278 | progress_bar = f"[{'=' * (task.progress // 5)}{' ' * (20 - task.progress // 5)}] {task.progress}%" |
| 279 | remaining_time = f", estimated time remaining: {task.running_left_time}s" if hasattr(task, 'running_left_time') and task.running_left_time is not None else "" |
| 280 | print(f"\rTask {task_id} is {task.status}. Progress: {progress_bar}{remaining_time}", end='', flush=True) |
| 281 | |
| 282 | # Calculate next polling interval based on estimated time remaining |
| 283 | if hasattr(task, 'running_left_time') and task.running_left_time is not None: |
| 284 | # Use 80% of the estimated remaining time as the next polling interval |
| 285 | polling_interval = max(2, task.running_left_time * 0.5) |
| 286 | else: |
| 287 | polling_interval = min(polling_interval * 2, 30) |
| 288 | # Wait before polling again |