如何使用 await 表达式?
How to use await expression?
无法弄清楚如何使用 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()
就像在 C# 中一样,await
只能在 async
方法(函数)中使用。
无法弄清楚如何使用 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()
就像在 C# 中一样,await
只能在 async
方法(函数)中使用。