Skip to content
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

Add GatheringTaskGroup class for Python 3.10 and below #52

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Next version

- [#52](https://github.com/sdss/sdsstools/pull/52) Add basic support for `GatheringTaskGroup` for Python versions 3.10 and below.

## [1.8.1](https://github.com/sdss/sdsstools/compare/1.8.0...1.8.1)

- Fixed import of `GatheringTaskGroup` for Python < 3.11.
Expand Down
47 changes: 47 additions & 0 deletions src/sdsstools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"get_temporary_file_path",
"run_in_executor",
"cancel_task",
"GatheringTaskGroup",
]


Expand Down Expand Up @@ -139,4 +140,50 @@

return [task.result() for task in self.__tasks]

else:

class GatheringTaskGroup:

Check warning on line 145 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L145

Added line #L145 was not covered by tests
"""Simple implementation of ``asyncio.TaskGroup`` for Python 3.10 and below.

The behaviour of this class is not exactly the same as ``asyncio.TaskGroup``,
especially when it comes to handling of exceptions during execution.

"""

def __init__(self):
self._tasks = []
self._joined: bool = False

Check warning on line 155 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L153-L155

Added lines #L153 - L155 were not covered by tests

def __repr__(self):
return f"<GatheringTaskGroup tasks={len(self._tasks)}>"

Check warning on line 158 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L157-L158

Added lines #L157 - L158 were not covered by tests

async def __aenter__(self):
self._joined = False
self._tasks = []

Check warning on line 162 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L160-L162

Added lines #L160 - L162 were not covered by tests

return self

Check warning on line 164 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L164

Added line #L164 was not covered by tests

async def __aexit__(self, exc_type, exc_value, traceback):

Check warning on line 166 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L166

Added line #L166 was not covered by tests
if exc_type is not None:
return False

Check warning on line 168 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L168

Added line #L168 was not covered by tests

await asyncio.gather(*self._tasks)
self._joined = True

Check warning on line 171 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L170-L171

Added lines #L170 - L171 were not covered by tests

def create_task(self, coro):

Check warning on line 173 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L173

Added line #L173 was not covered by tests
"""Creates a task and appends it to the list of tasks."""

task = asyncio.create_task(coro)
self._tasks.append(task)

Check warning on line 177 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L176-L177

Added lines #L176 - L177 were not covered by tests

return task

Check warning on line 179 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L179

Added line #L179 was not covered by tests

def results(self):

Check warning on line 181 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L181

Added line #L181 was not covered by tests
"""Returns the results of the tasks in the same order they were created."""

if not self._joined:
raise RuntimeError("Tasks have not been gathered yet.")

Check warning on line 185 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L185

Added line #L185 was not covered by tests

return [task.result() for task in self._tasks]

Check warning on line 187 in src/sdsstools/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L187

Added line #L187 was not covered by tests

__all__.append("GatheringTaskGroup")
8 changes: 1 addition & 7 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,20 @@
from __future__ import annotations

import asyncio
import sys
import warnings
from time import sleep

import pytest

from sdsstools.utils import (
GatheringTaskGroup,
Timer,
cancel_task,
get_temporary_file_path,
run_in_executor,
)


if sys.version_info >= (3, 11):
from sdsstools.utils import GatheringTaskGroup


def test_timer():
with Timer() as timer:
sleep(0.1)
Expand Down Expand Up @@ -101,7 +97,6 @@ async def test_cancel_task_None():
await cancel_task(task)


@pytest.mark.skipif(sys.version_info < (3, 11), reason="requires python3.11 or higher")
async def test_gathering_task_group():
async def _task(i):
await asyncio.sleep(0.1)
Expand All @@ -114,7 +109,6 @@ async def _task(i):
assert group.results() == list(range(10))


@pytest.mark.skipif(sys.version_info < (3, 11), reason="requires python3.11 or higher")
async def test_gathering_task_group_results_fails():
async def _task(i):
await asyncio.sleep(0.1)
Expand Down