def checkpoint_if_cancelled():
"""Issue a :ref:`checkpoint <checkpoints>` if the calling context has been
cancelled.
Equivalent to (but potentially more efficient than)::
if trio.current_deadline() == -inf:
await trio.hazmat.checkpoint()
This is either a no-op, or else it allow other tasks to be scheduled and
then raises :exc:`trio.Cancelled`.
Typically used together with :func:`cancel_shielded_checkpoint`.
"""
task = current_task()
if (task._pending_cancel_scope() is not None or
(task is task._runner.main_task and task._runner.ki_pending)):
await _core.checkpoint()
assert False # pragma: no cover
task._cancel_points += 1
评论列表
文章目录