asyncio-extensions 0.0.1__py3-none-any.whl

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.
@@ -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()
@@ -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
+ [![PyPI - Version](https://img.shields.io/pypi/v/asyncio-extensions.svg)](https://pypi.org/project/asyncio-extensions)
30
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/asyncio-extensions.svg)](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,8 @@
1
+ asyncio_extensions/__init__.py,sha256=TpsoAdtWockMsW1VRpoDL1hLb2i3T3zh8iTmM4ZZ28Y,311
2
+ asyncio_extensions/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ asyncio_extensions/taskgroups.py,sha256=GBYrYBgpuHJQBdIaElsRgg2GOVjpR_R8Cstuy6usuxM,1728
4
+ asyncio_extensions/utils.py,sha256=H3q1sqrZvw7owshrab65iXYQdQgArX_h7kFvKsyFjV0,618
5
+ asyncio_extensions-0.0.1.dist-info/METADATA,sha256=G2jOAJzF4AtcixXTtCsI6CW-UjN3POBql-qwmKRUG5c,1712
6
+ asyncio_extensions-0.0.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
7
+ asyncio_extensions-0.0.1.dist-info/licenses/LICENSE.txt,sha256=9MhvrEwVOMUhmjtwqlza75RnD32B8zRvqNygMxjI2YI,1131
8
+ asyncio_extensions-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.