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

相关推荐
谅望者5 分钟前
数据分析笔记07:Python编程语言介绍
大数据·数据库·笔记·python·数据挖掘·数据分析
Geo_V28 分钟前
LangChain Memory 使用示例
人工智能·python·chatgpt·langchain·openai·大模型应用·llm 开发
小呀小萝卜儿1 小时前
2025-11-15 学习记录--Python-LSTM模型定义(PyTorch)
python·学习·lstm
百锦再1 小时前
第15章 并发编程
android·java·开发语言·python·rust·django·go
laufing2 小时前
pyinstaller 介绍
python·构建打包
谅望者2 小时前
数据分析笔记09:Python条件语循环
笔记·python·数据分析
Auspemak-Derafru2 小时前
从U盘损坏中恢复视频文件并修复修改日期的完整解决方案
python
techzhi3 小时前
Intellij idea 注释模版
java·python·intellij-idea
李昊哲小课3 小时前
wsl ubuntu24.04 cuda13 cudnn9 pytorch 显卡加速
人工智能·pytorch·python·cuda·cudnn
温暖名字3 小时前
调用qwen3-omni的api对本地文件生成视频文本描述(批量生成)
python·音视频·qwen·qa问答