1import asyncio
2
3async def print_B(): #Simple async def
4 print("B")
5
6async def main_def():
7 print("A")
8 await asyncio.gather(print_B())
9 print("C")
10asyncio.run(main_def())
11
12# The function you wait for must include async
13# The function you use await must include async
14# The function you use await must run by asyncio.run(THE_FUNC())
15
16
17
1#will sleep the current corutien for set numner of seconds
2import asyncio
3await asyncio.sleep(1)
4
5
1import asyncio
2
3async def main():
4 print('Hello ...')
5 await asyncio.sleep(1)
6 print('... World!')
7
8# Python 3.7+
9asyncio.run(main())
10
11# output
12"""
13Hello ...
14...World!
15"""
16# with a 1 second wait time after Hello ...
1import asyncio
2from PIL import Image
3import urllib.request as urllib2
4
5async def getPic(): #Proof of async def
6 pic = Image.open(urllib2.urlopen("https://c.files.bbci.co.uk/E9DF/production/_96317895_gettyimages-164067218.jpg"))
7 return pic
8
9async def main_def():
10 print("A")
11 print("Must await before get pic0...")
12 pic0 = await asyncio.gather(getPic())
13 print(pic0)
14asyncio.run(main_def())
1async def get_chat_id(name):
2 await asyncio.sleep(3)
3 return "chat-%s" % name
4
5async def main():
6 id_coroutine = get_chat_id("django")
7 result = await id_coroutine
8