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
NextNext commit
Implement task cancel requests
  • Loading branch information
@Tinche
Tinche committedFeb 22, 2022
commita231718813e8d0a12849d8e21697d63632d880b9
16 changes: 7 additions & 9 deletionsLib/asyncio/tasks.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -105,7 +105,7 @@ def __init__(self, coro, *, loop=None, name=None):
else:
self._name = str(name)

self._cancel_requested =False
self._num_cancels_requested =0
self._must_cancel = False
self._fut_waiter = None
self._coro = coro
Expand DownExpand Up@@ -202,9 +202,9 @@ def cancel(self, msg=None):
self._log_traceback = False
if self.done():
return False
if self._cancel_requested:
self._num_cancels_requested += 1
if self._num_cancels_requested > 1:
return False
self._cancel_requested = True
if self._fut_waiter is not None:
if self._fut_waiter.cancel(msg=msg):
# Leave self._fut_waiter; it may be a Task that
Expand All@@ -217,14 +217,12 @@ 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 self._cancel_requested
return self._num_cancels_requested

def uncancel(self):
if self._cancel_requested:
self._cancel_requested = False
return True
else:
return False
if self._num_cancels_requested > 0:
self._num_cancels_requested -= 1
return self._num_cancels_requested

def __step(self, exc=None):
if self.done():
Expand Down
24 changes: 8 additions & 16 deletionsModules/_asynciomodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -91,7 +91,7 @@ typedef struct {
PyObject *task_context;
int task_must_cancel;
int task_log_destroy_pending;
inttask_cancel_requested;
inttask_num_cancels_requested;
} TaskObj;

typedef struct {
Expand DownExpand Up@@ -2036,7 +2036,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
Py_CLEAR(self->task_fut_waiter);
self->task_must_cancel = 0;
self->task_log_destroy_pending = 1;
self->task_cancel_requested = 0;
self->task_num_cancels_requested = 0;
Py_INCREF(coro);
Py_XSETREF(self->task_coro, coro);

Expand DownExpand Up@@ -2203,10 +2203,10 @@ _asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg)
Py_RETURN_FALSE;
}

if (self->task_cancel_requested) {
self->task_num_cancels_requested += 1;
if (self->task_num_cancels_requested > 1) {
Py_RETURN_FALSE;
}
self->task_cancel_requested = 1;

if (self->task_fut_waiter) {
PyObject *res;
Expand DownExpand Up@@ -2252,12 +2252,7 @@ _asyncio_Task_cancelling_impl(TaskObj *self)
/*[clinic end generated code: output=803b3af96f917d7e input=c50e50f9c3ca4676]*/
/*[clinic end generated code]*/
{
if (self->task_cancel_requested) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
return PyLong_FromLong(self->task_num_cancels_requested);
}

/*[clinic input]
Expand All@@ -2276,13 +2271,10 @@ _asyncio_Task_uncancel_impl(TaskObj *self)
/*[clinic end generated code: output=58184d236a817d3c input=5db95e28fcb6f7cd]*/
/*[clinic end generated code]*/
{
if (self->task_cancel_requested) {
self->task_cancel_requested = 0;
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
if (self->task_num_cancels_requested > 0) {
self->task_num_cancels_requested -= 1;
}
return PyLong_FromLong(self->task_num_cancels_requested);
}

/*[clinic input]
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp