Request that this task cancel itself. This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop. The coroutine then has a chance to clean up or even deny the request using try/except/finally. U
(self, msg=None)
| 196 | return base_tasks._task_print_stack(self, limit, file) |
| 197 | |
| 198 | def cancel(self, msg=None): |
| 199 | """Request that this task cancel itself. |
| 200 | |
| 201 | This arranges for a CancelledError to be thrown into the |
| 202 | wrapped coroutine on the next cycle through the event loop. |
| 203 | The coroutine then has a chance to clean up or even deny |
| 204 | the request using try/except/finally. |
| 205 | |
| 206 | Unlike Future.cancel, this does not guarantee that the |
| 207 | task will be cancelled: the exception might be caught and |
| 208 | acted upon, delaying cancellation of the task or preventing |
| 209 | cancellation completely. The task may also return a value or |
| 210 | raise a different exception. |
| 211 | |
| 212 | Immediately after this method is called, Task.cancelled() will |
| 213 | not return True (unless the task was already cancelled). A |
| 214 | task will be marked as cancelled when the wrapped coroutine |
| 215 | terminates with a CancelledError exception (even if cancel() |
| 216 | was not called). |
| 217 | |
| 218 | This also increases the task's count of cancellation requests. |
| 219 | """ |
| 220 | self._log_traceback = False |
| 221 | if self.done(): |
| 222 | return False |
| 223 | self._num_cancels_requested += 1 |
| 224 | # These two lines are controversial. See discussion starting at |
| 225 | # https://github.com/python/cpython/pull/31394#issuecomment-1053545331 |
| 226 | # Also remember that this is duplicated in _asynciomodule.c. |
| 227 | # if self._num_cancels_requested > 1: |
| 228 | # return False |
| 229 | if self._fut_waiter is not None: |
| 230 | if self._fut_waiter.cancel(msg=msg): |
| 231 | # Leave self._fut_waiter; it may be a Task that |
| 232 | # catches and ignores the cancellation so we may have |
| 233 | # to cancel it again later. |
| 234 | return True |
| 235 | # It must be the case that self.__step is already scheduled. |
| 236 | self._must_cancel = True |
| 237 | self._cancel_message = msg |
| 238 | return True |
| 239 | |
| 240 | def cancelling(self): |
| 241 | """Return the count of the task's cancellation requests. |
no test coverage detected