Skip to content

Commit b1269ae

Browse files
committed
Cleanup
1 parent 1276619 commit b1269ae

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

pymongo/_asyncio_task.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
from __future__ import annotations
2020

2121
import asyncio
22+
import sys
2223
from typing import Any, Coroutine, Optional
2324

2425

26+
# TODO (https://jira.mongodb.org/browse/PYTHON-4981): Revisit once the underlying cause of the swallowed cancellations is uncovered
2527
class _Task(asyncio.Task):
2628
def __init__(self, coro: Coroutine[Any, Any, Any], *, name: Optional[str] = None) -> None:
2729
super().__init__(coro, name=name)
@@ -32,9 +34,11 @@ def cancel(self, msg: Optional[str] = None) -> bool:
3234
self._cancelled = True
3335
return super().cancel(msg=msg)
3436

35-
def is_cancelled(self) -> bool:
37+
def cancelling(self) -> bool:
3638
return self._cancelled
3739

3840

39-
def create_task(coro: Coroutine[Any, Any, Any], *, name: Optional[str] = None) -> _Task:
41+
def create_task(coro: Coroutine[Any, Any, Any], *, name: Optional[str] = None) -> asyncio.Task:
42+
if sys.version_info >= (3, 11):
43+
return asyncio.create_task(coro, name=name)
4044
return _Task(coro, name=name)

pymongo/periodic_executor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import weakref
2424
from typing import Any, Optional
2525

26-
from pymongo._asyncio_task import _Task, create_task
26+
from pymongo._asyncio_task import create_task
2727
from pymongo.lock import _create_lock
2828

2929
_IS_SYNC = False
@@ -52,7 +52,7 @@ def __init__(
5252
self._min_interval = min_interval
5353
self._target = target
5454
self._stopped = False
55-
self._task: Optional[_Task] = None
55+
self._task: Optional[asyncio.Task] = None
5656
self._name = name
5757
self._skip_sleep = False
5858

@@ -64,7 +64,7 @@ def open(self) -> None:
6464
self._stopped = False
6565

6666
if self._task is None or (
67-
self._task.done() and not self._task.cancelled() and not self._task.is_cancelled()
67+
self._task.done() and not self._task.cancelled() and not self._task.cancelling() # type: ignore[unused-ignore, attr-defined]
6868
):
6969
self._task = create_task(self._run(), name=self._name)
7070

@@ -99,7 +99,7 @@ def skip_sleep(self) -> None:
9999

100100
async def _run(self) -> None:
101101
while not self._stopped:
102-
if self._task and self._task.is_cancelled():
102+
if self._task and self._task.cancelling(): # type: ignore[unused-ignore, attr-defined]
103103
raise asyncio.CancelledError
104104
try:
105105
if not await self._target():

0 commit comments

Comments
 (0)