Skip to content

Commit 452042d

Browse files
committed
stop() -> cancel()
1 parent 7754aad commit 452042d

4 files changed

Lines changed: 33 additions & 33 deletions

File tree

Doc/library/asyncio-task.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,21 @@ and reliable way to wait for all tasks in the group to finish.
342342

343343
Close the given coroutine if the task group is not active.
344344

345-
.. method:: stop()
345+
.. method:: cancel()
346346

347-
Stop the task group.
347+
Cancel the task group.
348348

349349
:meth:`~asyncio.Task.cancel` will be called on any tasks in the group that
350350
aren't yet done, as well as the parent (body) of the group. This will
351351
cause the task group context manager to exit *without*
352352
:exc:`asyncio.CancelledError` being raised.
353353

354-
If :meth:`stop` is called before entering the task group, the group will be
355-
stopped upon entry. This is useful for patterns where one piece of
354+
If :meth:`cancel` is called before entering the task group, the group will be
355+
cancelled upon entry. This is useful for patterns where one piece of
356356
code passes an unused :class:`asyncio.TaskGroup` instance to another in order to have
357-
the ability to stop anything run within the group.
357+
the ability to cancel anything run within the group.
358358

359-
:meth:`stop` is idempotent and may be called after the task group has
359+
:meth:`cancel` is idempotent and may be called after the task group has
360360
already exited.
361361

362362
.. versionadded:: next

Lib/asyncio/taskgroups.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self):
3636
self._errors = []
3737
self._base_error = None
3838
self._on_completed_fut = None
39-
self._stop_on_enter = False
39+
self._cancel_on_enter = False
4040

4141
def __repr__(self):
4242
info = ['']
@@ -63,8 +63,8 @@ async def __aenter__(self):
6363
raise RuntimeError(
6464
f'TaskGroup {self!r} cannot determine the parent task')
6565
self._entered = True
66-
if self._stop_on_enter:
67-
self.stop()
66+
if self._cancel_on_enter:
67+
self.cancel()
6868

6969
return self
7070

@@ -281,24 +281,24 @@ def _on_task_done(self, task):
281281
self._parent_cancel_requested = True
282282
self._parent_task.cancel()
283283

284-
def stop(self):
285-
"""Stop the task group
284+
def cancel(self):
285+
"""Cancel the task group
286286
287287
`cancel()` will be called on any tasks in the group that aren't yet
288288
done, as well as the parent (body) of the group. This will cause the
289-
task group context manager to exit *without* a Cancelled exception
289+
task group context manager to exit *without* `asyncio.CancelledError`
290290
being raised.
291291
292-
If `stop()` is called before entering the task group, the group will be
293-
stopped upon entry. This is useful for patterns where one piece of
292+
If `cancel()` is called before entering the task group, the group will be
293+
cancelled upon entry. This is useful for patterns where one piece of
294294
code passes an unused TaskGroup instance to another in order to have
295-
the ability to stop anything run within the group.
295+
the ability to cancel anything run within the group.
296296
297-
`stop()` is idempotent and may be called after the task group has
297+
`cancel()` is idempotent and may be called after the task group has
298298
already exited.
299299
"""
300300
if not self._entered:
301-
self._stop_on_enter = True
301+
self._cancel_on_enter = True
302302
return
303303
if self._exiting and not self._tasks:
304304
return

Lib/test/test_asyncio/test_taskgroups.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -997,66 +997,66 @@ class MyKeyboardInterrupt(KeyboardInterrupt):
997997
self.assertIsNotNone(exc)
998998
self.assertListEqual(gc.get_referrers(exc), no_other_refs())
999999

1000-
async def test_taskgroup_stop_children(self):
1000+
async def test_taskgroup_cancel_children(self):
10011001
# (asserting that TimeoutError is not raised)
10021002
async with asyncio.timeout(1):
10031003
async with asyncio.TaskGroup() as tg:
10041004
tg.create_task(asyncio.sleep(10))
10051005
tg.create_task(asyncio.sleep(10))
10061006
await asyncio.sleep(0)
1007-
tg.stop()
1007+
tg.cancel()
10081008

1009-
async def test_taskgroup_stop_body(self):
1009+
async def test_taskgroup_cancel_body(self):
10101010
count = 0
10111011
async with asyncio.TaskGroup() as tg:
1012-
tg.stop()
1012+
tg.cancel()
10131013
count += 1
10141014
await asyncio.sleep(0)
10151015
count += 1
10161016
self.assertEqual(count, 1)
10171017

1018-
async def test_taskgroup_stop_idempotent(self):
1018+
async def test_taskgroup_cancel_idempotent(self):
10191019
count = 0
10201020
async with asyncio.TaskGroup() as tg:
1021-
tg.stop()
1022-
tg.stop()
1021+
tg.cancel()
1022+
tg.cancel()
10231023
count += 1
10241024
await asyncio.sleep(0)
10251025
count += 1
10261026
self.assertEqual(count, 1)
10271027

1028-
async def test_taskgroup_stop_after_exit(self):
1028+
async def test_taskgroup_cancel_after_exit(self):
10291029
async with asyncio.TaskGroup() as tg:
10301030
await asyncio.sleep(0)
10311031
# (asserting that exception is not raised)
1032-
tg.stop()
1032+
tg.cancel()
10331033

1034-
async def test_taskgroup_stop_before_enter(self):
1034+
async def test_taskgroup_cancel_before_enter(self):
10351035
tg = asyncio.TaskGroup()
1036-
tg.stop()
1036+
tg.cancel()
10371037
count = 0
10381038
async with tg:
10391039
count += 1
10401040
await asyncio.sleep(0)
10411041
count += 1
10421042
self.assertEqual(count, 1)
10431043

1044-
async def test_taskgroup_stop_before_exception(self):
1044+
async def test_taskgroup_cancel_before_exception(self):
10451045
async def raise_exc(parent_tg: asyncio.TaskGroup):
1046-
parent_tg.stop()
1046+
parent_tg.cancel()
10471047
raise RuntimeError
10481048

10491049
with self.assertRaises(ExceptionGroup):
10501050
async with asyncio.TaskGroup() as tg:
10511051
tg.create_task(raise_exc(tg))
10521052
await asyncio.sleep(1)
10531053

1054-
async def test_taskgroup_stop_after_exception(self):
1054+
async def test_taskgroup_cancel_after_exception(self):
10551055
async def raise_exc(parent_tg: asyncio.TaskGroup):
10561056
try:
10571057
raise RuntimeError
10581058
finally:
1059-
parent_tg.stop()
1059+
parent_tg.cancel()
10601060

10611061
with self.assertRaises(ExceptionGroup):
10621062
async with asyncio.TaskGroup() as tg:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add :meth:`~asyncio.TaskGroup.stop`.
1+
Add :meth:`~asyncio.TaskGroup.cancel`.

0 commit comments

Comments
 (0)