在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)
这是Python 3.6针对上述代码引发的异常:
File "example.py", line 4
yield from open(tmp_file)
^
SyntaxError: 'yield from' inside async function