Skip to content

gh-150060: add asyncio.Task.cancelling_since() to expose first cancellation timestamp#150071

Open
prince8273 wants to merge 2 commits into
python:mainfrom
prince8273:asyncio-cancelling-since
Open

gh-150060: add asyncio.Task.cancelling_since() to expose first cancellation timestamp#150071
prince8273 wants to merge 2 commits into
python:mainfrom
prince8273:asyncio-cancelling-since

Conversation

@prince8273
Copy link
Copy Markdown

Summary

Add Task.cancelling_since() — a new method on asyncio.Task that returns
the event loop time (float) when cancel() was first called, or None if
cancellation has not been requested.

Motivation

Task.cancelling() exposes how many cancellation requests were made, but not
when the first request happened. This makes it impossible to measure
cancellation latency, debug timeout handling, or emit metrics in long-running
async systems.

Changes

  • Modules/_asynciomodule.c — new struct field, init, clear, traverse, first-cancel timestamp capture, Argument Clinic definition, method impl, method table entry
  • Modules/clinic/_asynciomodule.c.h — Clinic-generated wrapper
  • Lib/asyncio/tasks.py — pure-Python _PyTask mirror
  • Lib/test/test_asyncio/test_tasks.py — 3 regression tests
  • Doc/library/asyncio-task.rst — API docs with .. versionadded:: 3.16
  • Misc/NEWS.d/ — blurb entry for asyncio: add Task.cancelling_since() to expose first cancellation timestamp #150060

Smoke test

before: None
after: 68086.5663514
stable: True

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 19, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32756056 | 📁 Comparing 9724051 against main (ba0aca3)

  🔍 Preview build  

2 files changed
± library/asyncio-task.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant