Python 高级编程和异步IO并发编程 --12_8 async,await

# python为了将语义变得更加明确,就引入了async和await关键词用于定义原生的协程

async def downloader(url):
    return "Tom"

async def download_url(url):
    # do something
    html = await downloader(url)  # 将控制权交出去,等待值返回
    return html

if __name__=="__main__":
    coro = download_url("http://www.baidu.com")
    coro.send(None)
# python为了将语义变得更加明确,就引入了async和await关键词用于定义原生的协程
import types

@types.coroutine
async def downloader(url):
    yield "Tom"

async def download_url(url):
    # do something
    html = await downloader(url)  # 将控制权交出去,等待值返回
    return html

if __name__=="__main__":
    coro = download_url("http://www.baidu.com")
    coro.send(None)
发布了380 篇原创文章 · 获赞 129 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/f2157120/article/details/105234258