예제 #1
0
    async def _test():

        async with fanout.stream() as s1,\
                   fanout.stream() as s2,\
                   fanout.stream() as s3:

            s1 = islice(s1, 2)
            s2 = islice(s2, 2)
            s3 = islice(s3, 2)

            chained = chain(s1, s2, s3)
            items = [item async for item in chained]
            assert [1, 2, 1, 2, 1, 2] == items
            assert len(fanout._fanout_queues.data) == 3
예제 #2
0
async def test_islice_exact(slicing):
    """`isclice` consumes exactly as many items as needed"""
    boundary = slice(*slicing) if len(slicing) > 1 else slice(0, slicing[0])
    expected = list(range(boundary.stop)[boundary])
    assert (await a.list(
        a.islice(ayield_exactly(max(boundary.start, boundary.stop)),
                 *slicing)) == expected)
예제 #3
0
async def first(
    *activities: Coroutine[Any, Any, RT],
    count: Optional[int] = 1,
) -> AsyncIterator[RT]:
    """
    Run all ``activities`` concurrently to get the first ``count`` results available

    :param activities: activities to run concurrently
    :param count: maximum number of results
    :return: async iterable of results
    :raises usim.Concurrent: if any of the ``activities`` raise an exception
    :raises ValueError: if ``count`` is bigger than number of ``activities``

    If there are more results than ``count``,
    any remaining ``activities`` are aborted after yielding the last result.
    If there are less results than ``count``,
    the iterator finishes after yielding the last result.
    If ``count`` is :py:data:`None`, the iterator provides all results.

    Results are always yielded in the order of becoming available.
    The initial order of ``activities`` is irrelevant.
    """
    results: Queue[RT] = Queue()
    count = count if count is not None else len(activities)
    if count > len(activities):
        raise ValueError(
            f"cannot provide {count} results from {len(activities)} activities"
        )
    async with Scope() as scope:
        for activity in activities:
            scope.do(
                _first_monitor(activity, queue=results),
                volatile=True,
            )
        async for winner in a.islice(results, count):
            yield winner
예제 #4
0
 async def get_blocks(self, start, end):
     async for block in islice(self.blocks, start, end):
         if not isinstance(block, Block):
             block = await Block.from_dict(block)
         yield block
예제 #5
0
 async def get_block(self, start, end):
     return await anext(islice(self.blocks, start, end))
예제 #6
0
async def test_islice(iterable, slicing):
    expected = list(itertools.islice(iterable, *slicing))
    assert await a.list(a.islice(iterable, *slicing)) == expected
    assert await a.list(a.islice(asyncify(iterable), *slicing)) == expected