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去执行其他的字节码

相关推荐
Circ.14 小时前
文本相似性对比python代码
开发语言·python·相似度
Ahtacca14 小时前
基于决策树算法的动物分类实验:Mac环境复现指南
python·算法·决策树·机器学习·ai·分类
萌>__<新14 小时前
AI聊天助手-测试报告
人工智能·python
sg_knight14 小时前
设计模式实战:观察者模式(Observer)
python·观察者模式·设计模式
Humbunklung14 小时前
WMO 天气代码(Code Table 4677)深度解析与应用报告
开发语言·数据库·python
weixin_4492900114 小时前
uv打包Python为exe步骤
开发语言·python·uv
书到用时方恨少!15 小时前
Python threading 使用指南:并发编程的轻骑兵
python·多线程·thread·多任务
努力学习_小白15 小时前
数据增强——tensorflow
人工智能·python·tensorflow
m0_6948455715 小时前
marimo搭建教程:替代Jupyter的交互式开发工具
服务器·ide·python·docker·jupyter·github
csdn2015_15 小时前
Set<String> 类型取第一条记录
开发语言·windows·python