python入门系列十五(asyncio)

1.引言

python中asyncio是异步编程的代表,为什么会需要异步编程?当处理I/O密集型任务的时候,比如说查询数据库,文件操作,网络请求等等。同步方式会因为等待I/O操作而阻塞整个应用。asyncio的异步方案,相比较多线程,有效减少上下文切换的开销;相比较于多进程,资源消耗更少!是个不错的方案。

asyncio可以高效实现如下场景:

  • web服务:同时处理大量客户端请求
  • 爬虫:并行下载
  • 实时数据处理:高效处理多个数据流

2.案例

2.1.核心概念三要素

2.1.1.协程

asyncio关键步骤:

  • 通过async关键字定义协程函数(任务函数)
  • 通过await关键字调用协程函数
  • 通过asyncio.run方法启动执行
python 复制代码
import asyncio

# 定义协程函数
async def say_hello(name):
    # 模拟I/O等待
    await asyncio.sleep(1)
    print(f"Hello, {name}!")

# 运行协程
async def main():
    await say_hello("小王")
    await say_hello("老王")

# 运行
asyncio.run(main())

2.1.2.事件循环

python 复制代码
import asyncio

# 定义协程函数
async def say_hello(name):
    # 模拟I/O等待
    await asyncio.sleep(1)
    print(f"Hello, {name}!")

# 事件循环,通过事件循环执行任务
loop = asyncio.get_event_loop()
task = loop.create_task(say_hello("小王"))
loop.run_until_complete(task)
loop.close()

2.1.3.可等待对象

可等待对象有三类:

  • 协程对象:直接通过await调用
  • Task对象:并发执行多个协程
  • Future对象:底层异步操作容器

2.2.实践案例

设计一个模拟定时任务进度显示案例:

python 复制代码
import asyncio

# 定义协程函数:倒计时
async def countdown(number):
    while number > 0:
        print(f"剩余: {number}秒")
        await asyncio.sleep(0.1)
        number -= 1
    print("倒计时结束!")

# 定义协程函数:计算进度
async def progress_bar(total):
    for i in range(total+1):
        percent = i/total*100
        print(f"[{'#'*int(percent//2)}{' '*(50-int(percent//2))}] {percent:.1f}%")
        await asyncio.sleep(0.1)

async def main():
    await asyncio.gather(
        countdown(10),
        progress_bar(10)
    )

# 运行主协程
asyncio.run(main())

2.3.线程,进程,asyncio选择

相关推荐
databook1 天前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室1 天前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
飞哥数智坊1 天前
GPT-5-Codex 发布,Codex 正在取代 Claude
人工智能·ai编程
倔强青铜三1 天前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
虫无涯1 天前
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
人工智能
Dm_dotnet1 天前
公益站Agent Router注册送200刀额度竟然是真的
人工智能
算家计算1 天前
7B参数拿下30个世界第一!Hunyuan-MT-7B本地部署教程:腾讯混元开源业界首个翻译集成模型
人工智能·开源
用户2519162427111 天前
Python之语言特点
python
机器之心1 天前
LLM开源2.0大洗牌:60个出局,39个上桌,AI Coding疯魔,TensorFlow已死
人工智能·openai
刘立军1 天前
使用pyHugeGraph查询HugeGraph图数据
python·graphql