网站首页 > 技术文章 正文
最近在看一些开源代码中,经常会看到Python代码里面总有一些async,await之类的语法,难道我是老古董,之前没见过这种玩法?不过这玩意究竟能干点啥?还是自己亲身体验下才能理解深刻。
Python中的async和await是实现异步编程的关键语法,它们基于协程(coroutine)机制,让你可以在不使用传统线程和锁的情况下编写高效的并发代码。
异步编程允许程序在等待 I/O 操作(如网络请求、文件读写)完成时继续执行其他任务,而不是阻塞等待。这特别适合 I/O 密集型 场景(如爬虫、API 调用)。
下面是我使用python调用ollama API,对async功能进行测试:
这里我使用本地的Ollama API接口,现有模型如下:
使用requests的经典调用方式:
import requests
url = "http://10.133.254.123:11434/api/generate"
data = {
"model": "qwen2.5:7b",
"prompt": "鱼香肉丝的做法",
"stream": False
}
response = requests.post(url, json=data)
if response.status_code == 200:
result = response.json()
print("生成的文本:", result.get("response"))
else:
print("请求失败,状态码:", response.status_code)
print("错误信息:", response.text)
我在jupyter中跑出来的效果
下面是使用async后的
import asyncio
import aiohttp
async def call_ollama_async():
url = "http://localhost:11434/api/generate"
data = {
"model": "deepseek-r1:8b",
"prompt": "菠萝咕咾肉怎么做好吃?",
"stream": False
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data) as response:
if response.status == 200:
result = await response.json()
print("异步生成的文本:", result.get("response"))
else:
print("请求失败,状态码:", response.status)
print("错误信息:", await response.text())
asyncio.run(call_ollama_async())
这个直接在Jupyter中执行有报错:
asyncio.run() cannot be called from a running event loop
查了下出错原因,难道是在Jupyter中运行出现了嵌套调用?
- 避免嵌套调用:永远不要在一个已经运行的事件循环中调用 asyncio.run()。
- 使用 await 或 asyncio.gather():在异步函数中调用其他异步函数时,使用 await 或创建任务来并发执行。
- 多线程作为最后手段:只有在确实需要独立运行事件循环时,才考虑在新线程中创建事件循环。
将代码保存成py文件,使用python执行没有问题,效果如下:
直接结果:
当需要同时向 Ollama 接口发起多个请求时,async/await的优势就凸显出来了,如:
async def multiple_calls():
tasks = []
prompts = ["鱼香肉丝做法", "菠萝咕咾肉做法", "糖醋排骨的做法"]
for prompt in prompts:
task = asyncio.create_task(call_ollama_async(prompt))
tasks.append(task)
await asyncio.gather(*tasks)
使用async await函数还有其他优点:
- 代码可读性增强
对比同步代码和异步代码,async/await让异步代码的结构更接近同步代码的书写方式。
- 更好的错误处理
在异步函数中,可以使用try/except块来捕获异常,与同步代码的错误处理方式一致。
概括来说,在API调用场景中,使用async,await方式会有更好的效率,也让你写出的代码更像一位高手!
(本文完)
- 上一篇: Python中的数据导入与查询
- 下一篇: Python 文件操作与读取:从基础到进阶的全面指南
猜你喜欢
- 2025-08-03 Python内置模块:pathlib模块使用教程
- 2025-08-03 Python开发:Win10创建定时任务执行Python脚本
- 2025-08-03 Python 文件访问模式与定位操作:深度解析与实践应用
- 2025-08-03 Python中的paramiko库
- 2025-08-03 python中如何利用递归原理找出文件夹中的全部文件
- 2025-08-03 Python内置模块:shutil模块使用教程(文件与目录高级操作实践)
- 2025-08-03 Python之办公自动化文件操作
- 2025-08-03 python中12个文件处理高效技巧,不允许你还不知道
- 2025-08-03 Python-文件读写及异常处理
- 2025-08-03 Python 文件操作与读取:从基础到进阶的全面指南
- 08-06生产环境中使用的十大 Python 设计模式
- 08-06面试必备:Python内存管理机制(建议收藏)
- 08-06服务端开发面试必背——消息队列及它的主要用途和优点。附代码
- 08-06Python 栈:深度解析与应用
- 08-06Python中的多进程
- 08-06Python Logging 最佳实践
- 08-06Python并发数据结构实现原理
- 08-06用SendGrid和Redis队列用Python调度国际空间站的电子邮件
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)