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

相关推荐
吴佳浩6 小时前
大模型量化部署终极指南:让700亿参数的AI跑进你的显卡
人工智能·python·gpu
diegoXie7 小时前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言
七牛云行业应用7 小时前
解决OSError: No space left... 给DeepSeek Agent装上无限云硬盘
python·架构设计·七牛云·deepseek·agent开发
BoBoZz197 小时前
CutWithScalars根据标量利用vtkContourFilter得到等值线
python·vtk·图形渲染·图形处理
失散137 小时前
Python——1 概述
开发语言·python
萧鼎8 小时前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
qq_251533598 小时前
使用 Python 提取 MAC 地址
网络·python·macos
Data_agent10 小时前
学术爬虫实战:构建知网论文关键词共现网络的技术指南
python·算法
_一路向北_11 小时前
爬虫框架:Feapder使用心得
爬虫·python
皇族崛起11 小时前
【3D标注】- Unreal Engine 5.7 与 Python 交互基础
python·3d·ue5