码迷,mamicode.com
首页 > 其他好文 > 详细

【12.1】事件循环

时间:2019-08-17 18:26:45      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:==   异步io   for   channel   --   tornado   get   twisted   pytho   

---恢复内容开始---

技术图片

 

 

 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

 

waitgather

 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】事件循环

标签:==   异步io   for   channel   --   tornado   get   twisted   pytho   

原文地址:https://www.cnblogs.com/zydeboke/p/11369299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!