Create and track a task that will run in the pool. Args: coro: The coroutine function to create a task for. *args: Positional arguments for the coroutine. **kwargs: Keyword arguments for the coroutine. Returns: The created as
(self, coro: Callable, *args: Any, **kwargs: Any)
| 233 | return await coro(*args, **kwargs) |
| 234 | |
| 235 | def create_task(self, coro: Callable, *args: Any, **kwargs: Any) -> asyncio.Task: |
| 236 | """ |
| 237 | Create and track a task that will run in the pool. |
| 238 | |
| 239 | Args: |
| 240 | coro: The coroutine function to create a task for. |
| 241 | *args: Positional arguments for the coroutine. |
| 242 | **kwargs: Keyword arguments for the coroutine. |
| 243 | |
| 244 | Returns: |
| 245 | The created asyncio.Task object. |
| 246 | """ |
| 247 | # Create a task that runs the coroutine through the pool's 'run' method |
| 248 | task = asyncio.create_task(self.run(coro, *args, **kwargs)) |
| 249 | # Add the task to the tracking list |
| 250 | self.tasks.append(task) |
| 251 | # Add a callback to automatically remove the task from the list when it's done |
| 252 | task.add_done_callback(lambda t: self.tasks.remove(t)) |
| 253 | return task |
| 254 | |
| 255 | async def wait_all(self) -> None: |
| 256 | """Wait for all currently tracked tasks in the pool to complete.""" |