MCPcopy
hub / github.com/python-trio/trio / cancel

Method cancel

src/trio/_core/_run.py:937–953  ·  view source on GitHub ↗

Cancels this scope immediately. The optional ``reason`` argument accepts a string, which will be attached to any resulting :exc:`Cancelled` exception to help you understand where that cancellation is coming from and why it happened. This method is idempotent, i.e.,

(self, reason: str | None = None)

Source from the content-addressed store, hash-verified

935
936 @enable_ki_protection
937 def cancel(self, reason: str | None = None) -> None:
938 """Cancels this scope immediately.
939
940 The optional ``reason`` argument accepts a string, which will be attached to
941 any resulting :exc:`Cancelled` exception to help you understand where that
942 cancellation is coming from and why it happened.
943
944 This method is idempotent, i.e., if the scope was already
945 cancelled then this method silently does nothing.
946 """
947 try:
948 current_task = repr(_core.current_task())
949 except RuntimeError:
950 current_task = None
951 self._cancel(
952 CancelReason(reason=reason, source="explicit", source_task=current_task)
953 )
954
955 @property
956 def cancel_called(self) -> bool:

Calls 2

_cancelMethod · 0.95
CancelReasonClass · 0.85