小编Aki*_*esh的帖子

为什么我不能从'异步函数内'中获益?

在Python 3.6中,我可以yield在协程中使用.但是我无法使用yield from.

以下是我的代码.在第3行,我等待另一个协程.在第4行,我尝试yield from一个文件.为什么Python 3.6不允许我这样做?

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)
Run Code Online (Sandbox Code Playgroud)

以下是Python 3.6为上述代码引发的异常:

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

python python-3.x async-await python-asyncio python-3.6

15
推荐指数
1
解决办法
3116
查看次数