9

Does Python support functional-style operations on asynchronous iterators? I know that I can use map, filter and itertools to lazily transform and consume data coming from normal generators:

from itertools import accumulate, takewhile

def generator():
    a, b = 1, 1
    while True:
        yield a
        a, b = b, a + b

# create another iterator, no computation is started yet:
another_iterator = takewhile(lambda x: x < 100, accumulate(generator()))
# start consuming data:
print(list(another_iterator))
# [1, 2, 4, 7, 12, 20, 33, 54, 88]

Now, the same thing is not supported on Python 3.6's asynchronous generators/iterators because of course they do not implement the normal iterator protocol:

async def agenerator():
    a, b = 1, 1
    while True:
        yield a
        a, b = b, a + b

accumulate(agenerator())

TypeError: 'async_generator' object is not iterable

Is there some kind of async map or async itertools to achieve the similarly lazy behaviour in Python 3.6/3.7?

1 Answer 1

8

Most full asynchronous version of itertools I saw is aiostream module. Your example will be:

import asyncio
from aiostream.stream import takewhile, accumulate, list as alist


async def agenerator():
    a, b = 1, 1
    while True:
        yield a
        a, b = b, a + b


async def main():
    another_iterator = takewhile(
        accumulate(agenerator()),
        lambda x: x < 100, 
    )

    res = await alist(another_iterator)

    print(res)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this looks very interesting, especially the async version of list. I'm looking into the library, it seems fairly mature although it's at 0.3.0.
Is it still the best way to achieve this ? Is there no talk of including a standard way of handling async generators ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.