Python 3.5 - the name "wait" is undefined

I am trying to experiment with the new syntax awaitfor coroutines in Python 3.5.

I had a simple example:

#! /usr/bin/env python
import asyncio

@asyncio.coroutine
def adder(*args, delay):
    while True:
        yield from asyncio.sleep(delay)
        print(sum(args))


def main():
    asyncio.Task(adder(1, 2, 3, delay=5))
    asyncio.Task(adder(10, 20, delay=3))

    loop = asyncio.get_event_loop()
    loop.run_forever()

if __name__ == "__main__":
    main()

I changed the line yield fromto use the keyword await:

await asyncio.sleep(delay)

And I get SyntaxError:

  File "./test.py", line 8
    await asyncio.sleep(delay)
                ^
SyntaxError: invalid syntax

So, I'm trying to await (asyncio.sleep(delay))just see what happens:

Task exception was never retrieved
future: <Task finished coro=<adder() done, defined at c:\python35\Lib\asyncio\coroutines.py:198> exception=NameError("name 'await' is not defined",)>
Traceback (most recent call last):
  File "c:\python35\Lib\asyncio\tasks.py", line 239, in _step
    result = coro.send(value)
  File "c:\python35\Lib\asyncio\coroutines.py", line 200, in coro
    res = func(*args, **kw)
  File "./test.py", line 8, in adder
    await (asyncio.sleep(delay))
NameError: name 'await' is not defined
Task exception was never retrieved
future: <Task finished coro=<adder() done, defined at c:\python35\Lib\asyncio\coroutines.py:198> exception=NameError("name 'await' is not defined",)>
Traceback (most recent call last):
  File "c:\python35\Lib\asyncio\tasks.py", line 239, in _step
    result = coro.send(value)
  File "c:\python35\Lib\asyncio\coroutines.py", line 200, in coro
    res = func(*args, **kw)
  File "./test.py", line 8, in adder
    await (asyncio.sleep(delay))
NameError: name 'await' is not defined

Am I using the keyword incorrectly? Why awaitnot defined? I got the syntax for await this post.

Just to cover all my bases:

$ /usr/bin/env python --version

Python 3.5.0

Edit:

I suppose adding parens to a string is awaittrying to call a function await()- so why does this not work and gives me NameError. But why is the keyword not recognized in any of the examples?

+4
3

async await. yield from .

#! /usr/bin/env python
import asyncio

@asyncio.coroutine
async def adder(*args, delay):
    while True:
        await asyncio.sleep(delay)
        print(sum(args))


def main():
    asyncio.Task(adder(1, 2, 3, delay=5))
    asyncio.Task(adder(10, 20, delay=3))
    loop = asyncio.get_event_loop()
    loop.run_forever()

if __name__ == "__main__":
    main()
+6

async, . await , , async def. . PEP 492.

+1

Per PEP 492 await , async def, a __future__ import.

+1

Source: https://habr.com/ru/post/1611800/


All Articles