-
-
Notifications
You must be signed in to change notification settings - Fork 137
Just a concept of gather-like method #1966
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: master
Are you sure you want to change the base?
Changes from 3 commits
e09e762
dffe4f7
e858b8b
d1920b0
4b0f707
e959624
f35ce32
5bab4bc
231fa1e
957737e
965ee43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
from typing import Awaitable, Iterable | ||
|
||
import anyio | ||
|
||
from returns.future import FutureResult | ||
from returns.io import IOResult | ||
|
||
|
||
async def gather( | ||
containers: Iterable[ | ||
Awaitable, | ||
], | ||
) -> tuple[IOResult, ...]: | ||
""" | ||
Execute multiple coroutines concurrently and return their wrapped results. | ||
|
||
.. code:: python | ||
|
||
>>> import anyio | ||
>>> from returns.methods.gather import gather | ||
>>> from returns.io import IOSuccess | ||
|
||
>>> async def coro(): | ||
... return 1 | ||
>>> assert anyio.run(gather, [coro()]) == (IOSuccess(1), ) | ||
>>> container = FutureResult(coro()) | ||
>>> assert anyio.run(gather, [container.awaitable]) == (IOSuccess(1), ) | ||
|
||
""" | ||
async with anyio.create_task_group() as tg: | ||
containers_t = tuple(containers) | ||
ioresults: dict[int, IOResult] = {} | ||
|
||
async def _coro_wrapper(coro: Awaitable): # noqa: WPS430 | ||
try: | ||
return IOResult.from_value(await coro) | ||
except Exception as exc: | ||
return IOResult.from_failure(exc) | ||
|
||
async def _run_task(coro: Awaitable, index: int): # noqa: WPS430 | ||
ioresults[index] = await _coro_wrapper(coro) | ||
|
||
for coro_index, coro in enumerate(containers_t): | ||
tg.start_soon(_run_task, coro, coro_index) | ||
return tuple([ioresults[key] for key in sorted(ioresults.keys())]) | ||
RomanMIzulin marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
import pytest | ||
|
||
from returns.future import Future, FutureResult | ||
from returns.io import IO, IOResult | ||
from returns.methods import gather | ||
|
||
|
||
@pytest.mark.parametrize(('containers', 'expected'), [ | ||
( | ||
( | ||
Future.from_value(1), | ||
FutureResult.from_value(2), | ||
FutureResult.from_failure(None), | ||
), | ||
(IO(1), IOResult.from_value(2), FutureResult.from_failure(None)), | ||
), | ||
((), ()), | ||
]) | ||
def test_gather(containers, expected): | ||
"""Test partition function.""" | ||
assert gather(containers) == expected | ||
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. This should not be possible. You cannot mix 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. (from mypy's point of view, I mean) 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. It is not supposed to mix IO operations that can and can not fail? Or it is just mypy limitation? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create a new file:
async_.py
and put it there.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is naming of the method good(gather)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeap