python asyncio run_until_complete

import asyncio

def callback(loop, i):
    print("success time {} {}".format(i, loop.time()))

async def get_html(url):
    print("start get url")
    await asyncio.sleep(1)
    print("end get url")


# 两种创建的方法
if __name__ == "__main__":
    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"))
    loop.run_until_complete(task) # 接收的是一个future对象

 

posted @ 2019-07-21 11:23  下路派出所  阅读(3838)  评论(0编辑  收藏  举报