Python asyncio 的使用

本文详细介绍了Python的asyncio库,包括简单的使用、如何接收返回值、gather的运用、回调函数的处理、协程的取消以及协程的嵌套使用,全面解析了asyncio的关键特性。
摘要由CSDN通过智能技术生成

 asyncio 简单使用

# asyncio 是 python 用于解决一步 io 编程的解决方案

# 使用 asyncio
import asyncio
import time

async def get_html(url):
    print('start get url')
    # time.sleep(2)
    await asyncio.sleep(2)
    print('end get url')

if __name__ == '__main__':
    start = time.time()

    # 创建默认事件循环
    loop = asyncio.get_event_loop()
    tasks = [get_html('http://www.baidu.com') for i in range(10)]

    # 通过调用事件循环的 run_until_complete() 启动协程
    # asyncio.wait() 可以接受一个可迭代对象
    loop.run_until_complete(asyncio.wait(tasks))
    print(time.time()-start)

接受返回值的两种方法

# asyncio 是 python 用于解决一步 io 编程的解决方案

# 使用 asyncio
import asyncio
import time

async def get_html(url):
    print('start get url')
    # time.sleep(2)
    await asyncio.sleep(2)
    print('end get url')
    return '返回值'

if __name__ == '__main__':

    # 创建默认事件循环
    loop = asyncio.get_event_loop()

    # 用于接受返回值
    get_future = asyncio.ensure_future(get_html('http://www.baidu.com'))

    # 通过调用事件循环的 run_until_complete() 启动协程
    loop.run_until_complete(get_future)

    # 打印返回值
    print(get_future.result())

# 输出
start get url
end get url
返回值

gather 用法

# asyncio 是 python 用于解决一步 io 编程的解决方案

# 使用 asyncio
import asyncio
import time

async def get_html(url):
    # print('start get url')
    print(url)
    # time.sleep(2)
    await asyncio.sleep(2)
    print('end get url')

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    tasks = [get_html('http://www.baidu.com') for i in range(10)]

    # gather 和 wait 的区别
    # gather 更加高级
    group1 = [get_html('http://www.baidu.com') for i in range(2)]
    group2 = [get_html('http://www.baidu.com+++++') for i in range(2)]
    loop.run_until_complete(asyncio.gather(*group1,*group2))


    group1 = [get_html('http://www.baidu.com') for i in range(2)]
    group2 = [get_html('http://www.baidu.com+++++') for i in range(2)]
    g1 = asyncio.gather(*group1)
    g2 = asyncio.gather(*group2)
    loop.run_until_complete(asyncio.gather(g1, g2))


# 输出
http://www.baidu.com
http://www.baidu.com+++++
http://www.baidu.com+++++
http://www.baidu.com
end get url
end get url
end get url
end get url
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com+++++
http://www.baidu.com+++++
end get url
end get url
end get url
end get url
# asyncio 是 python 用于解决一步 io 编程的解决方案

# 使用 asyncio
import asyncio
import time

async def get_html(url):
    print('start get url')
    # time.sleep(2)
    await asyncio.sleep(2)
    print('end get url')
    return '返回值'

if __name__ == '__main__':

    # 创建默认事件循环
    loop = asyncio.get_event_loop()

    # 用于接受返回值
    task = loop.create_task(get_html('http://www.baidu.com'))
    
    # 通过调用事件循环的 run_until_complete() 启动协程
    loop.run_until_complete(task)

    # 打印返回值
    print(task.result())


# 输出
start get url
end get url
返回值

 回调函数

# asyncio 是 python 用于解决一步 io 编程的解决方案

# 使用 asyncio
import asyncio
import time
from functools import partial

async def get_html(url):
    print('start get url')
    # time.sleep(2)
    await asyncio.sleep(2)
    print('end get url')
    return '返回值'

# 回调函数
def callback(url,arg,future):
    print(url)
    print(arg)
    print('回调函数',future.result())

if __name__ == '__main__':

    # 创建默认事件循环
    loop = asyncio.get_event_loop()

    # 用于接受返回值
    get_future = asyncio.ensure_future(get_html('http://www.baidu.com'))

    # 回调函数 会把 get_future 传入到 callback
    # 注意参数位置 get_future 最后一个传进去的
    get_future.add_done_callback(partial(callback,'http://www.qq.com','鞍山打个方法'))

    # 通过调用事件循环的 run_until_complete() 启动协程
    loop.run_until_complete(get_future)

    # 打印返回值
    print('::::',get_future.result())


# 输出

start get url
end get url
http://www.qq.com
鞍山打个方法
回调函数 返回值
:::: 返回值

 


 取消协成

# asyncio 是 python 用于解决一步 io 编程的解决方案

# 使用 asyncio
import asyncio

async def get_html(num):
    print('开始')
    await asyncio.sleep(num)
    print('结束')

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:
        # 获取所有的task任务
        all_tasks = asyncio.Task.all_tasks()
        for i in all_tasks:
            print('========',i.cancel())
        loop.stop()
        loop.run_forever()
    finally:
        loop.close()

 


协成嵌套协成

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("%s + %s = %s" % (x, y, result))

loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()

 

import asyncio
import time
import aiohttp

start = time.time()

async def get(url,i):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as res:
            return res.status,i

async def hello(i):
    url = "http://www.taobao.com"
    print(i,url)
    res = await get(url,i)
    print('状态码: ',res)

loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(hello(i)) for i in range(50)]
loop.run_until_complete(asyncio.wait(tasks))

end = time.time()
print('Cost time:',end-start)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值