async await example python
Example 1: python async await
import asyncio
async def print_B():
print("B")
async def main_def():
print("A")
await asyncio.gather(print_B())
print("C")
asyncio.run(main_def())
Example 2: python async await
import asyncio
from PIL import Image
import urllib.request as urllib2
async def getPic():
pic = Image.open(urllib2.urlopen("https://c.files.bbci.co.uk/E9DF/production/_96317895_gettyimages-164067218.jpg"))
return pic
async def main_def():
print("A")
print("Must await before get pic0...")
pic0 = await asyncio.gather(getPic())
print(pic0)
asyncio.run(main_def())