This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author alexdelorenzo
Recipients alexdelorenzo, asvetlov, yselivanov
Date 2021-05-19.05:04:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1621400673.2.0.925525121843.issue44176@roundup.psfhosted.org>
In-reply-to
Content
According to the documentation, asyncio.as_completed() takes a positional argument, aws, as an iterable of awaitables[1]:

    asyncio.as_completed(aws, *, loop=None, timeout=None)
      Run awaitable objects in the aws iterable concurrently.

As seen in the attached as_completed_gen.py file, built-in containers like lists, and iterators over them, are accepted by as_completed() as the first parameter without raising an error.

However, as_completed() raises TypeError if it is called with an iterable of awaitables that is also a generator. There are examples of this behavior in as_completed_gen.py, but here is a short example using a generator expression in the main() coroutine function:

    from asyncio import run, as_completed
    
    async def example(): pass
    
    async def main():
      coros = (example() for _ in range(10))
    
      for coro in as_completed(coros):  # raises TypeError
        await coro
    
    run(main())

Running that example will raise a TypeError with this message:

    TypeError: expect an iterable of futures, not generator


If we look at the first line in the body of as_completed(), we can see why this error is thrown for generators that yield awaitables:

    def as_completed(fs, *, loop=None, timeout=None):
      if futures.isfuture(fs) or coroutines.iscoroutine(fs):
        raise TypeError(f"expect an iterable of futures, not {type(fs).__name__}")
      ...


Because generators are coroutines, and the first condition in as_completed() is True, and TypeError gets raised:

    from asyncio import coroutines

    # generators and generator expressions are coroutines
    assert coroutines.iscoroutine(example() for _ in range(10))


Perhaps as_completed() can use inspect.isawaitable() instead, like so:

    from inspect import isawaitable

    def as_completed(fs, *, loop=None, timeout=None):
      if futures.isfuture(fs) or isawaitable(fs):
        ...

I made a pull request with that change here[2].


[1] https://docs.python.org/3/library/asyncio-task.html#asyncio.as_completed

[2] https://github.com/python/cpython/pull/26228
History
Date User Action Args
2021-05-19 05:04:33alexdelorenzosetrecipients: + alexdelorenzo, asvetlov, yselivanov
2021-05-19 05:04:33alexdelorenzosetmessageid: <1621400673.2.0.925525121843.issue44176@roundup.psfhosted.org>
2021-05-19 05:04:33alexdelorenzolinkissue44176 messages
2021-05-19 05:04:32alexdelorenzocreate