asyncio-基本用法

python的asyncio协程模块的基本使用

1.代码示例

import asyncio

#创建一个协程函数,协程函数必须以async关键字修饰
async def request(url):
    print('正在向{}发送请求'.format(url))
    #IO阻塞(网络请求等都是IO阻塞),必须用await关键字修饰
    await asyncio.sleep(2)
    print('请求成功')

#获取协程对象
asy_obj = request('www.xxx.com')

#创建一个协程事件循环对象
loop = asyncio.get_event_loop()

#将协程对象添加到run_until_complete方法中
loop.run_until_complete(asy_obj)

2.create_task用法

import asyncio

#创建一个协程函数,协程函数必须以async关键字修饰
async def request(url):
    print('正在向{}发送请求'.format(url))
    #IO阻塞(网络请求等都是IO阻塞),必须用await关键字修饰
    await asyncio.sleep(2)
    print('请求成功')

#获取协程对象
asy_obj = request('www.xxx.com')

#创建一个协程事件循环对象
loop = asyncio.get_event_loop()

#通过事件循环对象,创建一个task任务,可以将任务添加到run_until_complete方法中去执行该任务
task= loop.create_task(asy_obj)
#将协程对象添加到run_until_complete方法中
loop.run_until_complete(task)

3.asyncio.ensure_future的用法

import asyncio

#创建一个协程函数,协程函数必须以async关键字修饰
async def request(url):
    print('正在向{}发送请求'.format(url))
    #IO阻塞(网络请求等都是IO阻塞),必须用await关键字修饰
    await asyncio.sleep(2)
    print('请求成功')

#获取协程对象
asy_obj = request('www.xxx.com')

#创建一个协程事件循环对象
loop = asyncio.get_event_loop()

#通过asyncio的ensure_future方法创建一个任务对象,次方法与create_task类似
task = asyncio.ensure_future(asy_obj)
#将协程对象添加到run_until_complete方法中
loop.run_until_complete(task)

4.给协程任务绑定回调函数

import asyncio

#创建一个协程函数,协程函数必须以async关键字修饰
async def request(url):
    print('正在向{}发送请求'.format(url))
    #IO阻塞(网络请求等都是IO阻塞),必须用await关键字修饰
    await asyncio.sleep(2)
    print('请求成功')
    return url

#定义一个回调函数
def callback(task):
    print(task.result())

#获取协程对象
asy_obj = request('www.xxx.com')

#创建一个协程事件循环对象
loop = asyncio.get_event_loop()

#通过asyncio的ensure_future方法创建一个任务对象,次方法与create_task类似
task = asyncio.ensure_future(asy_obj)

#给任务绑定一个回调函数,当任务执行完后,就会去执行绑定的回调函数
task.add_done_callback(callback)
#将协程对象添加到run_until_complete方法中
loop.run_until_complete(task)

5.执行多任务

import asyncio

#创建一个协程函数,协程函数必须以async关键字修饰
async def request(url):
    print('正在向{}发送请求'.format(url))
    #IO阻塞(网络请求等都是IO阻塞),必须用await关键字修饰
    await asyncio.sleep(2)
    print('请求成功')
    return url


#创建2个协程对象
asy_obj_1 = request('www.xxx.com')
asy_obj_2 = request('www.baidu.com')

#创建一任务列表:
task_list= [
    asyncio.ensure_future(asy_obj_1),
    asyncio.ensure_future(asy_obj_2)
        ]
#创建一个协程事件循环对象
loop = asyncio.get_event_loop()

#将协程任务列表添加到run_until_complete方法中,用asyncio.wait方法封装
loop.run_until_complete(asyncio.wait(task_list))

6.asyncio.run()的用法一:执行单个任务

import asyncio

#创建一个协程函数,协程函数必须以async关键字修饰
async def request(url):
    print('正在向{}发送请求'.format(url))
    #IO阻塞(网络请求等都是IO阻塞),必须用await关键字修饰
    await asyncio.sleep(2)
    print('请求成功')
    return url


#创建2个协程对象
asy_obj_1 = request('www.xxx.com')
asy_obj_2 = request('www.baidu.com')



#将协程对象传进去,就可以直接执行,不用创建事件循环对象
asyncio.run(asy_obj_1)

7.asyncio.run()的用法二:执行多个任务

import asyncio

#创建一个协程函数,协程函数必须以async关键字修饰
async def request(url):
    print('正在向{}发送请求'.format(url))
    #IO阻塞(网络请求等都是IO阻塞),必须用await关键字修饰
    await asyncio.sleep(2)
    print('请求成功')
    return url


#创建2个协程对象
asy_obj_1 = request('www.xxx.com')
asy_obj_2 = request('www.baidu.com')

async def main():
    task_list = [
        asyncio.ensure_future(asy_obj_1),
        asyncio.ensure_future(asy_obj_2)
    ]
    #done:用于接收已完成的任务; pending:用于接收未完成的任务 ; timeout:默认为None,如果传入,可以设置任务等待时间,如果超过时间,任务将被中断
    done,pending = await asyncio.wait(task_list,timeout=None)
    print('已完成',done)
    print('未完成',pending)

#mian()函数中封装了多个任务,只需要将main()传入run中,就可以执行多个任务
asyncio.run(main())
posted @   志强爱璇璇  阅读(234)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示