pytest - 多线程提速

python 复制代码
import time

def test1_test1():
    time.sleep(1)

    assert 1 == 1, "1==1"


def test1_test2():
    time.sleep(1)

    assert 1 == 1, "1==1"

上面2个函数,执行情况:

  • 正常执行时,花费 2.08s
  • 2个进程执行时,花费 1.18s
  • 2个线程执行时,花费 1.15s

问题:python有GIL(全局解释器锁),同一时刻只能执行一条线程的字节码指令,那么为什么pytest执行用例时,多线程会加快执行速度?

暂时得到的答案:time sleep 属于IO bound的任务。在IO bound的时候切换CPU去执行其他的字节码

相关推荐
aqi002 分钟前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵2 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf3 小时前
Agent 流程编排
后端·python·agent
copyer_xyf3 小时前
Agent RAG
后端·python·agent
copyer_xyf3 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf3 小时前
Agent 记忆管理
后端·python·agent
星云穿梭18 小时前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵19 小时前
用 Pygame 实现 15 puzzle
python·数学·游戏
黄忠1 天前
大模型之LangGraph技术体系
python·llm
hboot2 天前
AI工程师第二课 - 数据处理
人工智能·python·数据分析