python - 如何使用 await 表达式?

标签 python python-3.x async-await python-3.5

无法弄清楚如何在 python 3.5-rc2 中使用 await

>>> async def foo():
...     pass
... 
>>> await foo()
  File "<ipython-input-10-a18cb57f9337>", line 1
    await foo()
            ^
SyntaxError: invalid syntax

>>> c = foo()
>>> await c
  File "<ipython-input-12-cfb6bb0723be>", line 1
    await c
          ^
SyntaxError: invalid syntax

>>> import sys
>>> sys.version
'3.5.0rc2 (default, Aug 26 2015, 21:54:21) \n[GCC 5.2.0]'
>>> del c
RuntimeWarning: coroutine 'foo' was never awaited
>>> 

最佳答案

根据 documentation , await 只能在协程函数内部使用。所以使用它的正确语法应该是

async def foo():
    pass

async def bar():
    await foo()

关于python - 如何使用 await 表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239738/

相关文章:

python - 使用matplotlib制作动画并保存视频文件

python - 多维矩阵(/数组)的矩阵乘法 - 如何避免循环?

python - 如何从 pandas 数据框中找到调和平均速度

c# - 一次启动多个 async/await 函数并分别处理它们

python - 'int'对象没有属性python

python - 检查 csv 文件中的输入

python - 如何在 pygame 中创建 "while mouse down"循环

python - 将非空值向前传播到最后一个条目

c# - 如果线程正在等待异步操作完成,.NET Task 线程的资源是否会暂时返回池中?

c# - 如何合并多个异步调用的结果