From Python 3.5
was entered async/await
I'm implementing some objects that are waiting but I'm encountering doubts follow an example:
#!usr/bin/python3
# -*- coding: utf-8 -*-
import asyncio
class Waiting:
def __await__(self):
print('__await__')
yield from asyncio.sleep(3)
print('Ok')
async def main():
await Waiting()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()
If the script is executed the result will be:
__await__
Ok
Sure the script worked, but if you try to implement it using the new reserved words from Python 3.5
I run into an error, follow the script:
#!usr/bin/python3
# -*- coding: utf-8 -*-
import asyncio
class Waiting:
async def __await__(self):
print('__await__')
await asyncio.sleep(3)
print('Ok')
async def main():
await Waiting()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()
I made __await__
a co-routine and changed yield from
to await
and now when I run the script I get:
Traceback (most recent call last):
File "dunder_await.py", line 22, in <module>
loop.run_until_complete(main())
File "/usr/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
return future.result()
File "dunder_await.py", line 16, in main
await Waiting()
TypeError: __await__() returned a coroutine
Correct me if I am wrong but for me to use await
do I have to define a co-routine and await
should work as yield from
or am I wrong? And why am I getting this error?