Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-46771: Implement task cancel requests#31513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Tweak task docstrings
  • Loading branch information
@Tinche
Tinche committedFeb 24, 2022
commitab5bce9d1c3879b290cb5da517b771d86fca97e4
14 changes: 14 additions & 0 deletionsLib/asyncio/tasks.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -198,6 +198,8 @@ def cancel(self, msg=None):
task will be marked as cancelled when the wrapped coroutine
terminates with a CancelledError exception (even if cancel()
was not called).

This also increases the task's count of cancellation requests.
"""
self._log_traceback = False
if self.done():
Expand All@@ -217,9 +219,21 @@ def cancel(self, msg=None):
return True

def cancelling(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is thecancelling() the best name for returning the number or cancel requests?
It was good for bool flag, a counter needs better name IMHO.

ambv reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hm, do you have a suggestion? I was thinking that it's still usable as a "truthy" value. Usually when you're interested in the cancel count you should calluncancel() and use its return value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I have no good answer.
cancel_requests_count() describes exact meaning but the name is too long.
cancelling() can return bool but it looks like procrastination: why return less info than we really have.
The third option is droppingcancelling() method entirely, it is not required by the current asyncio code.
We can consider adding the method later on-demand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Note:.cancelling() doesn't exist in Python 3.10

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It's used in TaskGroup currently, so if we were to delete it we'd have to rewrite the code there in this same PR. It's also used byTask.__repr__() (via_task_repr_info()). But of those will work even though we upgraded it from a bool to an int. I'd say let's keep it for now, if we regret it we can rename or remove it before beta 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Deal!

"""Return the count of the task's cancellation requests.

This count is incremented when .cancel() is called
and may be decremented using .uncancel().
"""
return self._num_cancels_requested

def uncancel(self):
"""Decrement the task's count of cancellation requests.

This should be used by tasks that catch CancelledError
and wish to continue indefinitely until they are cancelled again.

Returns the remaining number of cancellation requests.
"""
if self._num_cancels_requested > 0:
self._num_cancels_requested -= 1
return self._num_cancels_requested
Expand Down
15 changes: 7 additions & 8 deletionsModules/_asynciomodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2191,6 +2191,8 @@ not return True (unless the task was already cancelled). A
task will be marked as cancelled when the wrapped coroutine
terminates with a CancelledError exception (even if cancel()
was not called).

This also increases the task's count of cancellation requests.
[clinic start generated code]*/

static PyObject *
Expand DownExpand Up@@ -2238,13 +2240,10 @@ _asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg)
/*[clinic input]
_asyncio.Task.cancelling

Return True if the task is in the process of being cancelled.

This is set once .cancel() is called
and remains set until .uncancel() is called.
Return the count of the task's cancellation requests.

As long as this flagisset, further .cancel()calls will be ignored,
until .uncancel() is called to reset it.
This countisincremented when .cancel()is called
and may be decremented using .uncancel().
[clinic start generated code]*/

static PyObject *
Expand All@@ -2258,12 +2257,12 @@ _asyncio_Task_cancelling_impl(TaskObj *self)
/*[clinic input]
_asyncio.Task.uncancel

Reset theflag returned by cancelling().
Decrement thetask's count of cancellation requests.

This should be used by tasks that catch CancelledError
and wish to continue indefinitely until they are cancelled again.

Returns theprevious value ofthe flag.
Returns theremaining number ofcancellation requests.
[clinic start generated code]*/

static PyObject *
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp