GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing github.com — bpo-44176: Allow `asyncio.as_completed()`'s first parameter to be a generator yielding awaitables by alexdelorenzo · Pull Request #26228 · python/cpython Conversation
|
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). Recognized GitHub usernameWe couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames: This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
asyncio.as_completed() first parameter to be a generator yielding awaitables
asyncio.as_completed() first parameter to be a generator yielding awaitablesasyncio.as_completed()'s first parameter to be a generator yielding awaitables
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Could you please also add tests to cover case when you pass generator to asyncio.as_completed and asyncio.wait function call?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
After investigation I think that actual problem is in From
But actually this function returns true for generators that is not coroutines: from asyncio import iscoroutine, run from types import coroutine @coroutine def coro(): yield def not_coro(): yield print(iscoroutine(coro())) # True print(iscoroutine(not_coro())) # True But if we will try to await async def main(): await coro() # ok await not_coro() # will fail with TypeError: object generator can't be used in 'await' expression In my opinion Lines 351 to 352 in 863154c
@Fidget-Spinner What is your opinion regarding this problem? Should we ping Yury and Andrew regarding this? UPD: While I was playing with def not_coro(): yield async def main(): await gather(not_coro()) # will not fail await not_coro() # will fail It looks like a bug, but I should do more investigation. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@uriyyo great investigation! That's a really interesting find. IMO, changing the behavior of I think we can ping Yury and Andrew politely on the bug tracker once OP adds tests like you mentioned. BTW, I use asyncio, but you probably have more knowledge about asyncio internals than me :). So this is a learning experience for me too (ie I might make mistakes). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Fidget-Spinner Ideal solution will be to change behavior of For instance currently, code bellow will not fail: await gather(None for _ in range(10)) When we have generator: def not_coro(): yield It's not valid to await it: await not_coro() # TypeError: object generator can't be used in 'await' expression But all cases below will not fail: await create_task(not_coro()) await ensure_future(not_coro()) await gather(not_coro()) await wait([not_coro()]) await wait([not_coro()]) await wait_for(not_coro(), 10) await shield(not_coro()) for f in as_completed([not_coro()]): print(await f) We can fix it be changing _COROUTINE_TYPES = (types.CoroutineType, collections.abc.Coroutine) _iscoroutine_typecache = set() def iscoroutine(obj): """Return True if obj is a coroutine object.""" if type(obj) in _iscoroutine_typecache: return True if isinstance(obj, _COROUTINE_TYPES): # Just in case we don't want to cache more than 100 # positive types. That shouldn't ever happen, unless # someone stressing the system on purpose. if len(_iscoroutine_typecache) < 100: _iscoroutine_typecache.add(type(obj)) return True elif isinstance(obj, types.GeneratorType) and obj.gi_code.co_flags & inspect.CO_ITERABLE_COROUTINE: return True else: return False But this solution is not backward compatible( |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Just ran into this issue myself and managed to find this PR. Looking forward to it being merged. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.