balki
balki

Reputation: 27684

How to use await expression?

Couldn't figure out how to use await from python 3.5-rc2

>>> 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
>>> 

Upvotes: 24

Views: 20739

Answers (2)

Melf
Melf

Reputation: 129

Just like in C#, await can only be used in an async method (function).

Upvotes: 7

Railslide
Railslide

Reputation: 5564

As per documentation, await can only be used inside a coroutine function. So the correct syntax for using it should be

async def foo():
    pass

async def bar():
    await foo()

Upvotes: 25

Related Questions