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选择

相关推荐
Yao.Li9 小时前
PVN3D ORT CUDA Custom Ops 实现与联调记录
人工智能·3d·具身智能
诺伦9 小时前
LocalClaw 在智能制造的新机会:6部门AI+电商政策下的工厂AI升级方案
人工智能·制造
疯狂成瘾者9 小时前
语义分块提升RAG检索精度
python
小陈工11 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
A__tao15 小时前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
墨染天姬15 小时前
【AI】端侧AIBOX可以部署哪些智能体
人工智能
研究点啥好呢16 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
AI成长日志16 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
2501_9481142416 小时前
2026年大模型API聚合平台技术评测:企业级接入层的治理演进与星链4SAPI架构观察
大数据·人工智能·gpt·架构·claude
小小工匠16 小时前
LLM - awesome-design-md 从 DESIGN.md 到“可对话的设计系统”:用纯文本驱动 AI 生成一致 UI 的新范式
人工智能·ui