asyncio-extensions 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- asyncio_extensions-0.0.1/.github/dependabot.yml +16 -0
- asyncio_extensions-0.0.1/.github/workflows/release.yml +33 -0
- asyncio_extensions-0.0.1/.github/workflows/tests.yml +58 -0
- asyncio_extensions-0.0.1/.gitignore +2 -0
- asyncio_extensions-0.0.1/LICENSE.txt +9 -0
- asyncio_extensions-0.0.1/PKG-INFO +47 -0
- asyncio_extensions-0.0.1/README.md +21 -0
- asyncio_extensions-0.0.1/pyproject.toml +154 -0
- asyncio_extensions-0.0.1/src/asyncio_extensions/__init__.py +12 -0
- asyncio_extensions-0.0.1/src/asyncio_extensions/py.typed +0 -0
- asyncio_extensions-0.0.1/src/asyncio_extensions/taskgroups.py +60 -0
- asyncio_extensions-0.0.1/src/asyncio_extensions/utils.py +27 -0
- asyncio_extensions-0.0.1/tests/__init__.py +0 -0
- asyncio_extensions-0.0.1/tests/test_taskgroups.py +54 -0
- asyncio_extensions-0.0.1/tests/test_utils.py +38 -0
- asyncio_extensions-0.0.1/tests/utils.py +2 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for more information:
|
|
4
|
+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
5
|
+
# https://containers.dev/guide/dependabot
|
|
6
|
+
|
|
7
|
+
version: 2
|
|
8
|
+
updates:
|
|
9
|
+
- package-ecosystem: "devcontainers"
|
|
10
|
+
directory: "/"
|
|
11
|
+
schedule:
|
|
12
|
+
interval: weekly
|
|
13
|
+
- package-ecosystem: "github-actions"
|
|
14
|
+
directory: "/"
|
|
15
|
+
schedule:
|
|
16
|
+
interval: weekly
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: Build & Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
deploy:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
environment:
|
|
14
|
+
name: release
|
|
15
|
+
url: https://pypi.org/p/asyncio-extensions
|
|
16
|
+
permissions:
|
|
17
|
+
id-token: write
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v6
|
|
20
|
+
|
|
21
|
+
- uses: actions/setup-python@v6
|
|
22
|
+
with:
|
|
23
|
+
python-version: "3.x"
|
|
24
|
+
|
|
25
|
+
- name: Install dependencies
|
|
26
|
+
run: |
|
|
27
|
+
python -m pip install --upgrade pip build
|
|
28
|
+
|
|
29
|
+
- name: Build package
|
|
30
|
+
run: python -m build
|
|
31
|
+
|
|
32
|
+
- name: Publish package
|
|
33
|
+
uses: pypa/gh-action-pypi-publish@v1.13.0
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
pull_request:
|
|
8
|
+
branches:
|
|
9
|
+
- main
|
|
10
|
+
|
|
11
|
+
concurrency:
|
|
12
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
13
|
+
cancel-in-progress: true
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
lint:
|
|
17
|
+
name: Run linters
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
strategy:
|
|
20
|
+
fail-fast: false
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v6
|
|
23
|
+
|
|
24
|
+
- name: Set up Python 3.13
|
|
25
|
+
uses: actions/setup-python@v6
|
|
26
|
+
with:
|
|
27
|
+
python-version: "3.13"
|
|
28
|
+
|
|
29
|
+
- name: Setup Hatch
|
|
30
|
+
run: pipx install hatch
|
|
31
|
+
|
|
32
|
+
- name: Run static analysis
|
|
33
|
+
run: hatch fmt --check
|
|
34
|
+
|
|
35
|
+
- name: Run type checking
|
|
36
|
+
run: hatch run mypy src/asyncio_extensions
|
|
37
|
+
|
|
38
|
+
tests:
|
|
39
|
+
name: Run tests on ${{ matrix.python-version }}
|
|
40
|
+
runs-on: ubuntu-latest
|
|
41
|
+
strategy:
|
|
42
|
+
fail-fast: false
|
|
43
|
+
matrix:
|
|
44
|
+
python-version: ["3.11", "3.12", "3.13", "3.14"]
|
|
45
|
+
steps:
|
|
46
|
+
- uses: actions/checkout@v6
|
|
47
|
+
|
|
48
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
49
|
+
uses: actions/setup-python@v6
|
|
50
|
+
with:
|
|
51
|
+
python-version: ${{ matrix.python-version }}
|
|
52
|
+
|
|
53
|
+
- name: Setup Hatch
|
|
54
|
+
run: pipx install hatch
|
|
55
|
+
|
|
56
|
+
- name: Run tests
|
|
57
|
+
run: |
|
|
58
|
+
hatch test -i py=${{ matrix.python-version }} --cover
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Christian Hartung <6785871+hartungstenio@users.noreply.github.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: asyncio-extensions
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Some (maybe) useful extensions for asyncio
|
|
5
|
+
Project-URL: Documentation, https://github.com/hartungstenio/asyncio-extensions#readme
|
|
6
|
+
Project-URL: Issues, https://github.com/hartungstenio/asyncio-extensions/issues
|
|
7
|
+
Project-URL: Source, https://github.com/hartungstenio/asyncio-extensions
|
|
8
|
+
Author-email: Christian Hartung <6785871+hartungstenio@users.noreply.github.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Keywords: asyncio,tasks
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.11
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# asyncio-extensions
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/asyncio-extensions)
|
|
30
|
+
[](https://pypi.org/project/asyncio-extensions)
|
|
31
|
+
|
|
32
|
+
-----
|
|
33
|
+
|
|
34
|
+
## Table of Contents
|
|
35
|
+
|
|
36
|
+
- [Installation](#installation)
|
|
37
|
+
- [License](#license)
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```console
|
|
42
|
+
pip install asyncio-extensions
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
`asyncio-extensions` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# asyncio-extensions
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/asyncio-extensions)
|
|
4
|
+
[](https://pypi.org/project/asyncio-extensions)
|
|
5
|
+
|
|
6
|
+
-----
|
|
7
|
+
|
|
8
|
+
## Table of Contents
|
|
9
|
+
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [License](#license)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```console
|
|
16
|
+
pip install asyncio-extensions
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## License
|
|
20
|
+
|
|
21
|
+
`asyncio-extensions` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "asyncio-extensions"
|
|
3
|
+
description = "Some (maybe) useful extensions for asyncio"
|
|
4
|
+
readme = "README.md"
|
|
5
|
+
requires-python = ">=3.11"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE.txt"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Christian Hartung", email = "6785871+hartungstenio@users.noreply.github.com" },
|
|
10
|
+
]
|
|
11
|
+
keywords = ["asyncio", "tasks"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 4 - Beta",
|
|
14
|
+
"Framework :: AsyncIO",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Python",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
22
|
+
"Programming Language :: Python :: Implementation :: PyPy",
|
|
23
|
+
"Topic :: Software Development :: Libraries",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = []
|
|
27
|
+
dynamic = ["version"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Documentation = "https://github.com/hartungstenio/asyncio-extensions#readme"
|
|
31
|
+
Issues = "https://github.com/hartungstenio/asyncio-extensions/issues"
|
|
32
|
+
Source = "https://github.com/hartungstenio/asyncio-extensions"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = [
|
|
36
|
+
"faker",
|
|
37
|
+
"mypy",
|
|
38
|
+
"pre-commit",
|
|
39
|
+
"pytest",
|
|
40
|
+
"pytest-asyncio",
|
|
41
|
+
"pytest-cov",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[build-system]
|
|
45
|
+
requires = ["hatch-vcs", "hatchling"]
|
|
46
|
+
build-backend = "hatchling.build"
|
|
47
|
+
|
|
48
|
+
[tool.coverage.run]
|
|
49
|
+
source_pkgs = ["asyncio_extensions", "tests"]
|
|
50
|
+
branch = true
|
|
51
|
+
parallel = true
|
|
52
|
+
omit = [
|
|
53
|
+
"src/asyncio_extensions/__about__.py",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[tool.coverage.paths]
|
|
57
|
+
asyncio_extensions = [
|
|
58
|
+
"src/asyncio_extensions",
|
|
59
|
+
"*/asyncio-extensions/src/asyncio_extensions"
|
|
60
|
+
]
|
|
61
|
+
tests = ["tests", "*/asyncio-extensions/tests"]
|
|
62
|
+
|
|
63
|
+
[tool.coverage.report]
|
|
64
|
+
exclude_lines = [
|
|
65
|
+
"no cov",
|
|
66
|
+
"if __name__ == .__main__.:",
|
|
67
|
+
"if TYPE_CHECKING:",
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
[tool.hatch.version]
|
|
71
|
+
source = "vcs"
|
|
72
|
+
|
|
73
|
+
[tool.hatch.envs.default]
|
|
74
|
+
dependency-groups = ["dev"]
|
|
75
|
+
|
|
76
|
+
[tool.hatch.envs.hatch-static-analysis]
|
|
77
|
+
config-path = "none"
|
|
78
|
+
dependencies = ["ruff==0.14.*"]
|
|
79
|
+
|
|
80
|
+
[tool.hatch.envs.hatch-test]
|
|
81
|
+
extra-dependencies = [
|
|
82
|
+
"faker",
|
|
83
|
+
"pytest-asyncio",
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
[[tool.hatch.envs.hatch-test.matrix]]
|
|
87
|
+
python = ["3.11", "3.12", "3.13", "3.14"]
|
|
88
|
+
|
|
89
|
+
[tool.pytest]
|
|
90
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
91
|
+
|
|
92
|
+
[tool.ruff]
|
|
93
|
+
target-version = "py311"
|
|
94
|
+
|
|
95
|
+
[tool.ruff.lint]
|
|
96
|
+
select = [
|
|
97
|
+
"A",
|
|
98
|
+
"ANN",
|
|
99
|
+
"ASYNC",
|
|
100
|
+
"B",
|
|
101
|
+
"BLE",
|
|
102
|
+
"C4",
|
|
103
|
+
"C90",
|
|
104
|
+
"COM818",
|
|
105
|
+
"D",
|
|
106
|
+
"DJ",
|
|
107
|
+
"DTZ",
|
|
108
|
+
"E",
|
|
109
|
+
"EM",
|
|
110
|
+
"ERA",
|
|
111
|
+
"EXE",
|
|
112
|
+
"F",
|
|
113
|
+
"FBT",
|
|
114
|
+
"FLY",
|
|
115
|
+
"FURB",
|
|
116
|
+
"G",
|
|
117
|
+
"I",
|
|
118
|
+
"ICN",
|
|
119
|
+
"INT",
|
|
120
|
+
"ISC",
|
|
121
|
+
"LOG",
|
|
122
|
+
"N",
|
|
123
|
+
"PERF",
|
|
124
|
+
"PGH",
|
|
125
|
+
"PIE",
|
|
126
|
+
"PL",
|
|
127
|
+
"PT",
|
|
128
|
+
"PTH",
|
|
129
|
+
"PYI",
|
|
130
|
+
"RET",
|
|
131
|
+
"RSE",
|
|
132
|
+
"RUF",
|
|
133
|
+
"S",
|
|
134
|
+
"SIM",
|
|
135
|
+
"SLF",
|
|
136
|
+
"SLOT",
|
|
137
|
+
"T10",
|
|
138
|
+
"T20",
|
|
139
|
+
"TC",
|
|
140
|
+
"TD",
|
|
141
|
+
"TID",
|
|
142
|
+
"TRY",
|
|
143
|
+
"UP",
|
|
144
|
+
"W",
|
|
145
|
+
]
|
|
146
|
+
|
|
147
|
+
[tool.ruff.lint.per-file-ignores]
|
|
148
|
+
"tests/*.py" = ["D", "S101"]
|
|
149
|
+
|
|
150
|
+
[tool.ruff.lint.pydocstyle]
|
|
151
|
+
convention = "pep257"
|
|
152
|
+
|
|
153
|
+
[tool.ruff.lint.flake8-tidy-imports]
|
|
154
|
+
ban-relative-imports = "parents"
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Utility functions and extensions for AsyncIO."""
|
|
2
|
+
|
|
3
|
+
from .taskgroups import TaskGroup, TerminateTaskGroup, force_terminate_task_group
|
|
4
|
+
from .utils import checkpoint, sleep_forever
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"TaskGroup",
|
|
8
|
+
"TerminateTaskGroup",
|
|
9
|
+
"checkpoint",
|
|
10
|
+
"force_terminate_task_group",
|
|
11
|
+
"sleep_forever",
|
|
12
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Extensions to interact with TaskGroups."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from collections.abc import Coroutine
|
|
5
|
+
from contextlib import AbstractAsyncContextManager
|
|
6
|
+
from contextvars import Context
|
|
7
|
+
from types import TracebackType
|
|
8
|
+
from typing import Any, Never, Self, TypeVar
|
|
9
|
+
|
|
10
|
+
T = TypeVar("T")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TerminateTaskGroup(BaseException):
|
|
14
|
+
"""Signal to terminate a task group.
|
|
15
|
+
|
|
16
|
+
Technically not an exception, so inherits from `BaseException`.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def force_terminate_task_group() -> Never:
|
|
21
|
+
"""Force termination of a task group."""
|
|
22
|
+
raise TerminateTaskGroup
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TaskGroup(AbstractAsyncContextManager["TaskGroup"]):
|
|
26
|
+
"""A version of asyncio.TaskGroup with a cancel method."""
|
|
27
|
+
|
|
28
|
+
def __init__(self) -> None: # noqa: D107
|
|
29
|
+
self.tasks = asyncio.TaskGroup()
|
|
30
|
+
|
|
31
|
+
async def __aenter__(self) -> Self: # noqa: D105
|
|
32
|
+
await self.tasks.__aenter__()
|
|
33
|
+
return self
|
|
34
|
+
|
|
35
|
+
async def __aexit__( # noqa: D105
|
|
36
|
+
self,
|
|
37
|
+
exc_type: type[BaseException] | None,
|
|
38
|
+
exc_value: BaseException | None,
|
|
39
|
+
traceback: TracebackType | None,
|
|
40
|
+
) -> bool | None:
|
|
41
|
+
try: # noqa: SIM105
|
|
42
|
+
await self.tasks.__aexit__(exc_type, exc_value, traceback)
|
|
43
|
+
except* TerminateTaskGroup:
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
def create_task(
|
|
49
|
+
self,
|
|
50
|
+
coro: Coroutine[Any, Any, T],
|
|
51
|
+
*,
|
|
52
|
+
name: str | None = None,
|
|
53
|
+
context: Context | None = None,
|
|
54
|
+
) -> asyncio.Task[T]:
|
|
55
|
+
"""Create a new task in this group and return it."""
|
|
56
|
+
return self.tasks.create_task(coro, name=name, context=context)
|
|
57
|
+
|
|
58
|
+
def cancel(self) -> None:
|
|
59
|
+
"""Cancel any remaining task in the TaskGroup."""
|
|
60
|
+
self.create_task(force_terminate_task_group())
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Utility functions."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Generator
|
|
4
|
+
from typing import Any, Never
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class YieldToEventLoop:
|
|
8
|
+
"""Helper class to give control back to the event loop."""
|
|
9
|
+
|
|
10
|
+
def __await__(self) -> Generator[None, Any, None]:
|
|
11
|
+
"""Yield control to the event loop."""
|
|
12
|
+
yield
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async def checkpoint() -> None:
|
|
16
|
+
"""
|
|
17
|
+
Give control back to the eventloop.
|
|
18
|
+
|
|
19
|
+
This has the same effect of and asyncio.sleep(0), but it is more semantic
|
|
20
|
+
"""
|
|
21
|
+
return await YieldToEventLoop()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
async def sleep_forever() -> Never:
|
|
25
|
+
"""Sleeps forever."""
|
|
26
|
+
while True:
|
|
27
|
+
await YieldToEventLoop()
|
|
File without changes
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from asyncio_extensions.taskgroups import (
|
|
6
|
+
TaskGroup,
|
|
7
|
+
TerminateTaskGroup,
|
|
8
|
+
force_terminate_task_group,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
from .utils import noop
|
|
12
|
+
|
|
13
|
+
pytestmark = pytest.mark.asyncio
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async def test_force_terminate_task_group() -> None:
|
|
17
|
+
with pytest.RaisesGroup(TerminateTaskGroup):
|
|
18
|
+
async with asyncio.TaskGroup() as tg:
|
|
19
|
+
tg.create_task(asyncio.sleep(1000))
|
|
20
|
+
tg.create_task(force_terminate_task_group())
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class TestTaskGroup:
|
|
24
|
+
async def test_runs_tasks(self) -> None:
|
|
25
|
+
async with TaskGroup() as tg:
|
|
26
|
+
tasks = [tg.create_task(noop()) for _ in range(10)]
|
|
27
|
+
|
|
28
|
+
assert all(t.done() for t in tasks)
|
|
29
|
+
assert not any(t.cancelled() for t in tasks)
|
|
30
|
+
|
|
31
|
+
async def test_cancel_with_running_task(self) -> None:
|
|
32
|
+
async with (
|
|
33
|
+
asyncio.timeout(1),
|
|
34
|
+
TaskGroup() as tg,
|
|
35
|
+
):
|
|
36
|
+
task = tg.create_task(asyncio.sleep(10))
|
|
37
|
+
tg.cancel()
|
|
38
|
+
assert task.cancelled() is True
|
|
39
|
+
|
|
40
|
+
async def test_cancel_without_running_task(self) -> None:
|
|
41
|
+
async with TaskGroup() as tg:
|
|
42
|
+
tg.cancel()
|
|
43
|
+
|
|
44
|
+
async def test_cancel_with_done_task(self) -> None:
|
|
45
|
+
async with (
|
|
46
|
+
asyncio.timeout(1),
|
|
47
|
+
TaskGroup() as tg,
|
|
48
|
+
):
|
|
49
|
+
task = tg.create_task(noop())
|
|
50
|
+
await task
|
|
51
|
+
tg.cancel()
|
|
52
|
+
|
|
53
|
+
assert task.done() is True
|
|
54
|
+
assert task.cancelled() is False
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from faker import Faker
|
|
5
|
+
|
|
6
|
+
from asyncio_extensions.utils import checkpoint, sleep_forever
|
|
7
|
+
|
|
8
|
+
from .utils import noop
|
|
9
|
+
|
|
10
|
+
pytestmark = pytest.mark.asyncio
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
async def test_checkpoint() -> None:
|
|
14
|
+
async with asyncio.TaskGroup() as tg:
|
|
15
|
+
task = tg.create_task(noop())
|
|
16
|
+
|
|
17
|
+
await checkpoint()
|
|
18
|
+
|
|
19
|
+
assert task.done() is True
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async def test_sleep_forever(faker: Faker) -> None:
|
|
23
|
+
with pytest.raises(TimeoutError):
|
|
24
|
+
async with asyncio.timeout(faker.random_int(1, 10)):
|
|
25
|
+
await sleep_forever()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async def test_sleep_forever_cycles_event_loop(faker: Faker) -> None:
|
|
29
|
+
with pytest.raises(TimeoutError): # noqa: PT012
|
|
30
|
+
async with (
|
|
31
|
+
asyncio.timeout(faker.random_int(1, 10)),
|
|
32
|
+
asyncio.TaskGroup() as tg,
|
|
33
|
+
):
|
|
34
|
+
tasks = [tg.create_task(asyncio.sleep(i)) for i in range(7)]
|
|
35
|
+
await sleep_forever()
|
|
36
|
+
|
|
37
|
+
assert all(t.done() for t in tasks)
|
|
38
|
+
assert not any(t.cancelled() for t in tasks)
|