Why can't I 'yield from' inside an async function?

十年热恋 提交于 2020-06-10 02:19:36

问题


In Python 3.6, I am able to use yield inside a coroutine. However I am not able to use yield from.

Below is my code. On line 3 I await another coroutine. On line 4 I try to yield from a file. Why won't Python 3.6 allow me to do that?

async def read_file(self, filename):
    with tempfile.NamedTemporaryFile(mode='r', delete=True, dir='/tmp', prefix='sftp') as tmp_file:
        await self.copy_file(filename, tmp_file)
        yield from open(tmp_file)

Here's the exception Python 3.6 raises for the above code:

  File "example.py", line 4
    yield from open(tmp_file)
    ^
SyntaxError: 'yield from' inside async function

回答1:


According to PEP 525, which introduces asyncronous generators in Python 3.6:

Asynchronous yield from

While it is theoretically possible to implement yield from support for asynchronous generators, it would require a serious redesign of the generators implementation.

yield from is also less critical for asynchronous generators, since there is no need provide a mechanism of implementing another coroutines protocol on top of coroutines. And to compose asynchronous generators a simple async for loop can be used:

async def g1():
    yield 1
    yield 2

async def g2():
    async for v in g1():
        yield v

As you can see, the answer boils down to "it would be too hard to implement, and you don't need it anyway".



来源:https://stackoverflow.com/questions/47376408/why-cant-i-yield-from-inside-an-async-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!