Create a new task in this group and return it. Similar to `asyncio.create_task`.
(self, coro, **kwargs)
| 180 | |
| 181 | |
| 182 | def create_task(self, coro, **kwargs): |
| 183 | """Create a new task in this group and return it. |
| 184 | |
| 185 | Similar to `asyncio.create_task`. |
| 186 | """ |
| 187 | if not self._entered: |
| 188 | coro.close() |
| 189 | raise RuntimeError(f"TaskGroup {self!r} has not been entered") |
| 190 | if self._exiting and not self._tasks: |
| 191 | coro.close() |
| 192 | raise RuntimeError(f"TaskGroup {self!r} is finished") |
| 193 | if self._aborting: |
| 194 | coro.close() |
| 195 | raise RuntimeError(f"TaskGroup {self!r} is shutting down") |
| 196 | task = self._loop.create_task(coro, **kwargs) |
| 197 | |
| 198 | futures.future_add_to_awaited_by(task, self._parent_task) |
| 199 | |
| 200 | # Always schedule the done callback even if the task is |
| 201 | # already done (e.g. if the coro was able to complete eagerly), |
| 202 | # otherwise if the task completes with an exception then it will cancel |
| 203 | # the current task too early. gh-128550, gh-128588 |
| 204 | self._tasks.add(task) |
| 205 | task.add_done_callback(self._on_task_done) |
| 206 | try: |
| 207 | return task |
| 208 | finally: |
| 209 | # gh-128552: prevent a refcycle of |
| 210 | # task.exception().__traceback__->TaskGroup.create_task->task |
| 211 | del task |
| 212 | |
| 213 | # Since Python 3.8 Tasks propagate all exceptions correctly, |
| 214 | # except for KeyboardInterrupt and SystemExit which are |