How to use 'async def' in Python 3.6 version? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to use 'async def' in Python 3.6 version?

I can't understand 'async def' and 'await'. I need some one tell me what's that, how to let them doing to work? Maybe I speak English is not good, But I wish you help me by example.Thans all.

29th Apr 2019, 5:09 PM
周書祥
周書祥 - avatar
4 Answers
+ 4
Ah, asyncio.run() will only work in Python 3.7. In Python 3.6 it's a little more complicated: loop = asyncio.get_event_loop() loop.run_until_complete(a()) loop.close()
30th Apr 2019, 12:08 PM
Anna
Anna - avatar
+ 7
Async stands for asynchronous. It means that a code is not executed line by line, but a piece of code might pause and wait for another chunk of code (the awaitable) to be executed first. Here's an example: import asyncio async def a(): await b() print('world') async def b(): await asyncio.sleep(1) print('hello', end=' ') asyncio.run(a()) In the last line, the async function a() is called. The first thing that a() does is wait for b() to be executed. That's what the "await" keyword does. There's another awaitable in b() (asyncio.sleep() which will make b() pause for one second). So a() will wait for b() to finish, b() will wait for a second and then print "hello", then a() will continue and print "world".
29th Apr 2019, 5:26 PM
Anna
Anna - avatar
+ 1
Attribute Error:module "asyncio" has no attribute "run"
30th Apr 2019, 12:05 PM
周書祥
周書祥 - avatar
0
Is b() awaitable or a() ? Anna
8th May 2019, 8:44 PM
harshit
harshit - avatar