MCPcopy Index your code
hub / github.com/python/cpython / cancel

Method cancel

Lib/asyncio/tasks.py:183–223  ·  view source on GitHub ↗

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. Unlike F

(self, msg=None)

Source from the content-addressed store, hash-verified

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

Callers 15

_add_readerMethod · 0.45
_remove_readerMethod · 0.45
_add_writerMethod · 0.45
_remove_writerMethod · 0.45
putMethod · 0.45
getMethod · 0.45
__main__.pyFile · 0.45
connection_lostMethod · 0.45
_on_shutdown_completeMethod · 0.45
_on_completionFunction · 0.45

Calls 1

doneMethod · 0.95

Tested by

no test coverage detected