Skip to content

Commit

Permalink
Add GatheringTaskGroup class for Python 3.10 and below
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Sep 12, 2024
1 parent f050824 commit 5bf7d21
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
46 changes: 46 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,49 @@ def results(self):

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:
raise RuntimeError("An error occurred in the task group.")

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)

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

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L170

Added line #L170 was not covered by tests

def create_task(self, coro):

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

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L172

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

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

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

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L175-L176

Added lines #L175 - L176 were not covered by tests

return task

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

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L178

Added line #L178 was not covered by tests

async def results(self):

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

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L180

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

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

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

View check run for this annotation

Codecov / codecov/patch

src/sdsstools/utils.py#L184

Added line #L184 was not covered by tests

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

__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

0 comments on commit 5bf7d21

Please sign in to comment.