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

相关推荐
Wellijohn1 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
bufengwangluo2 小时前
爬虫和调用 API 有什么区别?为什么优先选 API
python·网络爬虫·ip·api调用
Dmz丶2 小时前
前端转 AI 100 天 Day 54:RAG 项目实战(三)——多轮对话 + 会话管理
python
console.log('npc')2 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
geovindu4 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu5 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
真上帝的左手5 小时前
2. 编程语言-Python-Anaconda
开发语言·python
aifannihao5 小时前
断言从 == 改成 > 0:用 Python 审出 git diff 里的测试降级
git·python·测试·标准库·代码审查
高洁015 小时前
具身智能的人机协作与物理空间安全
人工智能·python·深度学习·机器学习·django