python3.5之后,才引入了原生协程的语法
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 # python为了将语义变得更加明确,就引入了async和await关键词用于定义原生的协程 5 6 7 async def downloader(url): 8 return 'zy' 9 10 11 # 在async中不能yield,await只能出现在async中 12 async def download_url(url): 13 # do somethings 14 html = await downloader(url) 15 16 return html 17 18 19 if __name__ == '__main__': 20 coro = download_url('http://www.imooc.com') 21 # next(coro)不能使用这种方式调用 22 coro.send(None)
Traceback (most recent call last): File "C:/Users/Administrator/Python/imooc/async_ts/async_await.py", line 22, in <module> coro.send(None) StopIteration: zy