asyncio 学习
#事件循环+回调(驱动生成器)+epoll(IO多路复用) #asyncio是python用于解决异步io编程的一整套解决方案 #tornado、gevent、twisted(scrapy, django channels) #torando(实现web服务器), django+flask(uwsgi, gunicorn+nginx) #tornado可以直接部署, nginx+tornado #使用asyncio # import asyncio # import time # async def get_html(url): # print("start get url") # await asyncio.sleep(2) # print("end get url") # # if __name__ == "__main__": # start_time = time.time() # loop = asyncio.get_event_loop() # tasks = [get_html("http://www.imooc.com") for i in range(10)] # loop.run_until_complete(asyncio.wait(tasks)) # print(time.time()-start_time) #获取协程的返回值 # import asyncio # import time # from functools import partial #给函数添加参数 # async def get_html(url): # print("start get url") # await asyncio.sleep(2) # return "bobby" # # def callback(url, future): # print(url) # print("send email to bobby") # # if __name__ == "__main__": # start_time = time.time() # loop = asyncio.get_event_loop() # # get_future = asyncio.ensure_future(get_html("http://www.imooc.com")) # task = loop.create_task(get_html("http://www.imooc.com")) # task.add_done_callback(partial(callback, "http://www.imooc.com")) # loop.run_until_complete(task) # print(task.result()) #wait 和 gather import asyncio import time async def get_html(url): print("start get url") await asyncio.sleep(2) print("end get url") if __name__ == "__main__": start_time = time.time() loop = asyncio.get_event_loop() tasks = [get_html("http://www.imooc.com") for i in range(10)] # loop.run_until_complete(asyncio.gather(*tasks)) # print(time.time()-start_time) #gather和wait的区别 #gather更加high-level 可以分组运行,分组取消 group1 = [get_html("http://projectsedu.com") for i in range(2)] group2 = [get_html("http://www.imooc.com") for i in range(2)] group1 = asyncio.gather(*group1) group2 = asyncio.gather(*group2) group2.cancel() loop.run_until_complete(asyncio.gather(group1, group2)) print(time.time() - start_time)
-
event_loop 事件循环:程序开启一个无限循环,把一些函数注册到事件循环上,当满足事件发生的时候,调用相应的协程函数
-
coroutine 协程:协程对象,指一个使用async关键字定义的函数,它的调用不会立即执行函数,而是会返回一个协程对象。协程对象需要注册到事件循环,由事件循环调用。
-
task 任务:一个协程对象就是一个原生可以挂起的函数,任务则是对协程进一步封装,其中包含了任务的各种状态
-
future: 代表将来执行或没有执行的任务的结果。它和task上没有本质上的区别
-
async/await 关键字:python3.5用于定义协程的关键字,async定义一个协程,await用于挂起阻塞的异步调用接口。
task取消和子协程调用原理
#1. run_until_complete # import asyncio # loop = asyncio.get_event_loop() # loop.run_forever() # loop.run_until_complete() #1. loop会被放到future中 #2. 取消future(task) # import asyncio # import time # # async def get_html(sleep_times): # print("waiting") # await asyncio.sleep(sleep_times) # print("done after {}s".format(sleep_times)) # # # if __name__ == "__main__": # task1 = get_html(2) # task2 = get_html(3) # task3 = get_html(3) # # tasks = [task1, task2, task3] # # loop = asyncio.get_event_loop() # # try: # loop.run_until_complete(asyncio.wait(tasks)) # except KeyboardInterrupt as e: # all_tasks = asyncio.Task.all_tasks() # for task in all_tasks: # print("cancel task") # print(task.cancel()) # loop.stop() # loop.run_forever()#必须添加,否则会有异常 # finally: # loop.close() #注册协程到loop中有两种方法 #1. 通过ensure_future/create_task #2. await