事件循环+回调(驱动生成器)+ epoll(IO多路复用)
asyncio 是python用于解决异步IO编程的一整套解决方案
import asyncio
import time
async def get_html(url):
print("get url start")
await asyncio.sleep(2)
print("get url end")
return 'dylan'
if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop() # 开始循环
tasks = [get_html('http://www.baidu.com') for i in range(10)]
loop.run_until_complete(asyncio.wait(tasks)) # asyncio.wait或者asyncio.gather是用于多任务,相当于多线程中的join方式,如果单个任务可以写成以下方式:
# loop.run_until_complete(get_html('http://www.baidu.com'))
print(time.time() - start_time)# 从协程中获取返回值
if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop()
get_return = asyncio.ensure_future(get_html('http://www.baidu.com'))
loop.run_until_complete(get_return)
print(get_return.result())或者:
if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop()
# get_return = asyncio.ensure_future(get_html('http://www.baidu.com'))
task = loop.create_task(get_html('http://www.baidu.com'))
loop.run_until_complete(task)
print(task.result())
添加协程运作完后运作
from functools import partial
def callback(message, future):
print('send email to someone {}'.format(message))if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop()
# get_return = asyncio.ensure_future(get_html('http://www.baidu.com'))
task = loop.create_task(get_html('http://www.baidu.com'))
task.add_done_callback(partial(callback, 'dylan'))
# get_return.add_done_callback(partial(callback, 'dylan'))
loop.run_until_complete(task)
print(task.result())
# asyncio.wait和asyncio.gather的区别
gather比wait更高级
而调用方式:
wait:
if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop()
tasks = [get_html('http://www.baidu.com') for i in range(10)]
loop.run_until_complete(asyncio.wait(tasks))
print(time.time() - start_time)gather:
if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop()
tasks = [get_html('http://www.baidu.com') for i in range(10)]
loop.run_until_complete(asyncio.gather(*tasks))
print(time.time() - start_time)# 多组任务
if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop()
# tasks = [get_html('http://www.baidu.com') for i in range(10)]
group1 = [get_html('http://www.baidu.com') for i in range(2)]
group2 = [get_html('http://www.qq.com') for i in range(10)]
# group1 = asyncio.gather(*group1)
# group2 = asyncio.gather(*group2)
loop.run_until_complete(asyncio.gather(*group1, *group2))
print(time.time() - start_time)if __name__ == "__main__":
start_time = time.time()
loop = asyncio.get_event_loop()
# tasks = [get_html('http://www.baidu.com') for i in range(10)]
group1 = [get_html('http://www.baidu.com') for i in range(2)]
group2 = [get_html('http://www.qq.com') for i in range(10)]
group1 = asyncio.gather(*group1)
group2 = asyncio.gather(*group2)
loop.run_until_complete(asyncio.gather(group1, group2))
print(time.time() - start_time)版权声明:本文为dylan_liang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。