24

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

2 Answers 2

25

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()
Sign up to request clarification or add additional context in comments.

2 Comments

If I have to use the coroutine, I have to create a function and then await on it to get the value?
It doesn't necessarily have to be a function, as long as it is an awaitable object (i.e. native and generator-based coroutine or an object with the magic method __await__) @balki
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.