摘要: import asyncio async def compute(x, y): print("Compute %s + %s ..." % (x, y)) await asyncio.sleep(1.0) return x + y async def print_sum(x, y): result = await compute(x, y) print... 阅读全文
posted @ 2019-07-20 16:50 下路派出所 阅读(1499) 评论(0) 推荐(0) 编辑
摘要: 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__": ... 阅读全文
posted @ 2019-07-20 16:36 下路派出所 阅读(2537) 评论(0) 推荐(0) 编辑
摘要: 1. wait, 等待某某执行完成以后才执行下一步 2. gather 比wait更加高级,可以将任务分组,并且取消掉,取消时,必须设置 return_exception为True,不然会抛异常 阅读全文
posted @ 2019-07-20 11:35 下路派出所 阅读(5731) 评论(0) 推荐(2) 编辑
摘要: 1. 获取协程返回值,实质就是future中的task 2. 使用loop自带的create task, 获取返回值 3. 使用callback,只要await地方的内容一运行完,就会运行callback 使用partial这个模块向callback函数中传入值 阅读全文
posted @ 2019-07-20 10:50 下路派出所 阅读(16189) 评论(1) 推荐(1) 编辑
摘要: import asyncio import time async def get_html(url): print("start get url") await asyncio.sleep(2) # 不能使用time.sleep(),这样的话是同步,就不是异步;await就相当于yield from print("end get url") if __name__ ==... 阅读全文
posted @ 2019-07-20 10:32 下路派出所 阅读(2396) 评论(0) 推荐(0) 编辑