1.async await
# python为了将语义变得更加明确,就引入了async和await关键词用于定义原生的协程
import types
async def downloader(url):
return "bobby"
@types.coroutine
def downloader(url):
yield "bobby"
async def download_url(url):
# dosomethings
html = await downloader(url) # await 耗时的操作交个其他函数去处理 在等待函数返回值过程中可以处理其他事情, async配合着await使用
return html
if __name__ == "__main__":
coro = download_url("http://www.imooc.com")
# next(None) # 会抛异常
coro.send(None)