12.1事件循环
Posted _simpleSmile
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12.1事件循环相关的知识,希望对你有一定的参考价值。
---恢复内容开始---
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 # 事件循环+回调(驱动生成器)+epoll(IO多路复用) 5 # asyncio是python用于解决异步io编程的一整套解决方案 6 # tornado、gevent、twisted(scrapy,django channels) 7 8 # 使用asyncio 9 import asyncio 10 import time 11 12 13 async def get_html(url): 14 """ 15 声明一个协程 16 """ 17 print(\'start get url\') 18 await asyncio.sleep(2) 19 print(\'end get url\') 20 21 22 if __name__ == \'__main__\': 23 start_time = time.time() 24 # 创建loop 25 loop = asyncio.get_event_loop() 26 tasks = [get_html(\'http://www.imooc.com\') for i in range(10)] 27 # 将任务提交 28 loop.run_until_complete(asyncio.wait(tasks)) 29 print(time.time() - start_time)
start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url 2.0041933059692383
获取协程的返回值
1 import asyncio 2 import time 3 from functools import partial 4 5 6 async def get_html(url): 7 """ 8 声明一个协程 9 """ 10 print(\'start get url\') 11 await asyncio.sleep(2) 12 print(\'end get url\') 13 return \'zy\' 14 15 16 def callback(url, future): 17 print(url) 18 print(\'send email to zy\') 19 20 21 if __name__ == \'__main__\': 22 start_time = time.time() 23 # 创建loop 24 loop = asyncio.get_event_loop() 25 get_future = asyncio.ensure_future(get_html(\'http://www.imooc.com\')) 26 # tasks = loop.create_task(get_html(\'http://www.imooc.com\')) 27 # 28 get_future.add_done_callback(partial(callback, \'http://www.imooc.com\')) 29 # 将任务提交 30 loop.run_until_complete(get_future) 31 # 获取返回值 32 print(get_future.result()) 33 print(time.time() - start_time)
start get url end get url http://www.imooc.com send email to zy zy 2.0022480487823486
wait和gather
1 # wait和gather 2 import asyncio 3 import time 4 5 6 async def get_html(url): 7 """ 8 声明一个协程 9 """ 10 print(\'start get url\') 11 await asyncio.sleep(2) 12 print(\'end get url\') 13 14 15 if __name__ == \'__main__\': 16 start_time = time.time() 17 # 创建loop 18 loop = asyncio.get_event_loop() 19 # tasks = [get_html(\'http://www.imooc.com\') for i in range(10)] 20 # # 一次提交多个任务 21 # loop.run_until_complete(asyncio.gather(*tasks)) 22 23 # wait和gather的区别 24 # gather更加high-level 25 group1 = [get_html(\'http://www.imooc.com\') for i in range(10)] 26 group2 = [get_html(\'http://www.baidu.com\') for i in range(10)] 27 group1 = asyncio.gather(*group1) 28 group2 = asyncio.gather(*group2) 29 30 loop.run_until_complete(asyncio.gather(group1, group2)) 31 print(time.time() - start_time)
start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url start get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url end get url 2.0028679370880127
以上是关于12.1事件循环的主要内容,如果未能解决你的问题,请参考以下文章
Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段