-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-108951: add TaskGroup.cancel() #127214
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
8ec2f60
907f1d0
2cfa1e6
bce1fb1
7754aad
452042d
f077241
8345647
7235c20
243db79
e36a1af
5ae5ee9
c83d853
5bba130
1ce0b40
e8617b4
8710b95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
belm0 marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,12 @@ | |
|
|
||
| import sys | ||
| import gc | ||
|
|
||
|
belm0 marked this conversation as resolved.
Outdated
|
||
| import asyncio | ||
| import contextvars | ||
| import contextlib | ||
| from asyncio import taskgroups | ||
| import math | ||
| import unittest | ||
| import warnings | ||
|
|
||
|
|
@@ -997,6 +999,69 @@ class MyKeyboardInterrupt(KeyboardInterrupt): | |
| self.assertIsNotNone(exc) | ||
| self.assertListEqual(gc.get_referrers(exc), no_other_refs()) | ||
|
|
||
| async def test_taskgroup_stop_children(self): | ||
|
belm0 marked this conversation as resolved.
Outdated
|
||
| async with asyncio.TaskGroup() as tg: | ||
| tg.create_task(asyncio.sleep(math.inf)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe these tasks should look like this? async def task(results, num):
results.append(num)
await asyncio.sleep(math.inf)
results.append(-num)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we can assert what was in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this particular test, I chose a different test approach, which is to wrap in For the other tests using |
||
| tg.create_task(asyncio.sleep(math.inf)) | ||
| await asyncio.sleep(0) | ||
| tg.stop() | ||
|
|
||
| async def test_taskgroup_stop_body(self): | ||
| count = 0 | ||
| async with asyncio.TaskGroup() as tg: | ||
| tg.stop() | ||
| count += 1 | ||
| await asyncio.sleep(0) | ||
| count += 1 | ||
| self.assertEqual(count, 1) | ||
|
|
||
| async def test_taskgroup_stop_idempotent(self): | ||
| count = 0 | ||
| async with asyncio.TaskGroup() as tg: | ||
| tg.stop() | ||
| tg.stop() | ||
| count += 1 | ||
| await asyncio.sleep(0) | ||
| count += 1 | ||
| self.assertEqual(count, 1) | ||
|
|
||
| async def test_taskgroup_stop_after_exit(self): | ||
| async with asyncio.TaskGroup() as tg: | ||
| await asyncio.sleep(0) | ||
| tg.stop() | ||
|
|
||
| async def test_taskgroup_stop_before_enter(self): | ||
| tg = asyncio.TaskGroup() | ||
| tg.stop() | ||
| count = 0 | ||
| async with tg: | ||
| count += 1 | ||
| await asyncio.sleep(0) | ||
| count += 1 | ||
| self.assertEqual(count, 1) | ||
|
|
||
| async def test_taskgroup_stop_before_exception(self): | ||
| async def raise_exc(parent_tg: asyncio.TaskGroup): | ||
| parent_tg.stop() | ||
| raise RuntimeError | ||
|
|
||
| with self.assertRaises(ExceptionGroup): | ||
| async with asyncio.TaskGroup() as tg: | ||
| tg.create_task(raise_exc(tg)) | ||
| await asyncio.sleep(1) | ||
|
|
||
| async def test_taskgroup_stop_after_exception(self): | ||
| async def raise_exc(parent_tg: asyncio.TaskGroup): | ||
| try: | ||
| raise RuntimeError | ||
| finally: | ||
| parent_tg.stop() | ||
|
|
||
| with self.assertRaises(ExceptionGroup): | ||
| async with asyncio.TaskGroup() as tg: | ||
| tg.create_task(raise_exc(tg)) | ||
|
gvanrossum marked this conversation as resolved.
|
||
| await asyncio.sleep(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add :meth:`~asyncio.TaskGroup.stop`. |
Uh oh!
There was an error while loading. Please reload this page.