-
Notifications
You must be signed in to change notification settings - Fork 17
fix: remove aiostream as a dependency #56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import asyncio | ||
| from collections.abc import AsyncGenerator | ||
| from typing import Any, TypeVar | ||
|
|
||
| from typing_extensions import Sentinel | ||
|
|
||
| _T = TypeVar("_T") | ||
| _SENTINEL = Sentinel("_SENTINEL") | ||
|
|
||
|
|
||
| async def merge_async_iterables( | ||
| *iterables: AsyncGenerator[_T, None], | ||
| ) -> AsyncGenerator[_T, None]: | ||
| """ | ||
| Merge multiple async generators into a single one. | ||
|
|
||
| Items are yielded as soon as they're produced by any of the source generators, in the order they arrive. | ||
|
|
||
| :param iterables: async generators to merge. | ||
| :yields: items produced by any of the source generators. | ||
| """ | ||
| queue: asyncio.Queue[Any] = asyncio.Queue() | ||
|
|
||
| async def _pump(iterable: AsyncGenerator[_T, None]) -> None: | ||
| try: | ||
| async for item in iterable: | ||
| await queue.put(item) | ||
| except BaseException as exc: | ||
| await queue.put(exc) | ||
| else: | ||
| await queue.put(_SENTINEL) | ||
|
|
||
| tasks = [asyncio.ensure_future(_pump(iterable)) for iterable in iterables] | ||
| remaining = len(tasks) | ||
| try: | ||
| while remaining: | ||
| item = await queue.get() | ||
| if item is _SENTINEL: | ||
| remaining -= 1 | ||
| elif isinstance(item, BaseException): | ||
| raise item | ||
| else: | ||
| yield item | ||
| finally: | ||
| for task in tasks: | ||
| task.cancel() | ||
| await asyncio.gather(*tasks, return_exceptions=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import asyncio | ||
| from collections.abc import AsyncGenerator | ||
|
|
||
| import pytest | ||
|
|
||
| from taskiq_aio_pika.utils import merge_async_iterables | ||
|
|
||
|
|
||
| async def _gen(*items: int, delay: float = 0.0) -> AsyncGenerator[int, None]: | ||
| for item in items: | ||
| if delay: | ||
| await asyncio.sleep(delay) | ||
| yield item | ||
|
|
||
|
|
||
| async def test_when_multiple_generators_passed__then_all_items_are_yielded() -> None: | ||
| result = [ | ||
| item | ||
| async for item in merge_async_iterables( | ||
| _gen(1, 2, 3), | ||
| _gen(4, 5), | ||
| ) | ||
| ] | ||
|
|
||
| assert sorted(result) == [1, 2, 3, 4, 5] | ||
|
|
||
|
|
||
| async def test_when_single_generator_passed__then_its_items_are_yielded_in_order() -> ( | ||
| None | ||
| ): | ||
| result = [item async for item in merge_async_iterables(_gen(1, 2, 3))] | ||
|
|
||
| assert result == [1, 2, 3] | ||
|
|
||
|
|
||
| async def test_when_no_generators_passed__then_nothing_is_yielded() -> None: | ||
| result = [item async for item in merge_async_iterables()] | ||
|
|
||
| assert result == [] | ||
|
|
||
|
|
||
| async def test_when_one_generator_raises__then_exception_is_propagated() -> None: | ||
| async def failing_gen() -> AsyncGenerator[int, None]: | ||
| yield 1 | ||
| raise ValueError("boom") | ||
|
|
||
| with pytest.raises(ValueError, match="boom"): | ||
| async for _ in merge_async_iterables(failing_gen(), _gen(2, delay=1)): | ||
| pass | ||
|
|
||
|
|
||
| async def test_when_one_generator_raises__then_other_generators_are_cancelled() -> None: | ||
| other_was_cancelled = False | ||
|
|
||
| async def slow_gen() -> AsyncGenerator[int, None]: | ||
| nonlocal other_was_cancelled | ||
| try: | ||
| await asyncio.sleep(10) | ||
| yield 1 | ||
| except asyncio.CancelledError: | ||
| other_was_cancelled = True | ||
| raise | ||
|
|
||
| async def failing_gen() -> AsyncGenerator[int, None]: | ||
| yield 1 | ||
| raise ValueError("boom") | ||
|
|
||
| with pytest.raises(ValueError, match="boom"): | ||
| async for _ in merge_async_iterables(failing_gen(), slow_gen()): | ||
| pass | ||
|
|
||
| assert other_was_cancelled | ||
|
|
||
|
|
||
| async def test_when_consumer_stops_early__then_remaining_generators_are_cancelled() -> ( | ||
| None | ||
| ): | ||
| other_was_cancelled = False | ||
|
|
||
| async def slow_gen() -> AsyncGenerator[int, None]: | ||
| nonlocal other_was_cancelled | ||
| try: | ||
| await asyncio.sleep(10) | ||
| yield 1 | ||
| except asyncio.CancelledError: | ||
| other_was_cancelled = True | ||
| raise | ||
|
|
||
| merged = merge_async_iterables(_gen(1), slow_gen()) | ||
| try: | ||
| async for _ in merged: | ||
| break | ||
| finally: | ||
| await merged.aclose() | ||
|
|
||
| assert other_was_cancelled |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.