V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sbldehanhan
V2EX  ›  Python

如何让 Python 实现真正多线程?

  •  
  •   sbldehanhan · 351 天前 · 3703 次点击
    这是一个创建于 351 天前的主题,其中的信息可能已经有所发展或是发生改变。

    concurrent 这个模块是真正多线程吗?

    23 条回复    2023-05-18 16:29:48 +08:00
    liuxingdeyu
        1
    liuxingdeyu  
       351 天前
    新版本已经去掉 gil 了,话说你为啥要用多线程,为了用 cpu ?
    githmb
        2
    githmb  
       351 天前
    不是号称胶水语言吗?粘一下 C 语言啊,fork 或者 pthread_create 一下
    roundgis
        3
    roundgis  
       351 天前 via Android
    CPython thread 可是真 thread
    westoy
        4
    westoy  
       351 天前   ❤️ 1
    GIL 被妖魔化的太厉害了

    python 的多线程就是 pthread

    只是 python 有 GIL 锁会卡 PyObject 的操作

    但是这个主要是卡纯 python 的

    PyObject 转 C/CPP 的模块里是可以释放 GIL 的

    所以大部分 C/CPP 写的库都是会释放 GIL 的, 包括原生 IO

    但是因为早年 linux 没有线程, 之后有段时间线程也是基于 fork 的,2.6 的时候又大改过一次, 所以用的人并不多, 导致主流的 Linux 下面都提倡 IO 异步并发, 计算的直接 fork, 用线程的并不多.....
    matrix1010
        5
    matrix1010  
       351 天前
    @liuxingdeyu 你这是大新闻, 没有 GIL 的 Python 可以算作另一种语言了
    sbldehanhan
        6
    sbldehanhan  
    OP
       351 天前
    @githmb 但是如果线程的那个函数里面又调用了其他 Python 函数,那被调用的函数也得用 C 实现一遍。这样工作量就太大了。
    sbldehanhan
        7
    sbldehanhan  
    OP
       351 天前
    @liuxingdeyu 为了快,现在太慢了。
    r6cb
        8
    r6cb  
       351 天前
    Alias4ck
        9
    Alias4ck  
       351 天前
    @liuxingdeyu 并没有 ,faster cpython 组 3.12 目标( https://github.com/faster-cpython/ideas/wiki/Python-3.12-Goals) 中 提到的[pep554]( https://peps.python.org/pep-0554/)也只是一个 draft 而且实现也是仅限于 C API 而且 pep554 还在讨论中( https://discuss.python.org/t/pep-554-multiple-interpreters-in-the-stdlib/24855/12)

    另外实现`nogil`的是另一个 pep,有兴趣去看看[pep703]( https://peps.python.org/pep-0703/) 还只是一个 draft 虽然他给出了[实现]( https://github.com/colesbury/nogil)。
    liuxingdeyu
        10
    liuxingdeyu  
       350 天前
    @matrix1010 @Alias4ck 额,是我表达有误了,我想表达的就是 3.12 的这个改版,去掉了很多关于核心使用的限制
    ihciah
        11
    ihciah  
       350 天前 via iPhone
    换 Mojo
    gitopen
        12
    gitopen  
       350 天前
    @r6cb ➕1
    hertzry
        13
    hertzry  
       350 天前
    multiprocessing 凑活能用。
    pollux
        14
    pollux  
       350 天前
    @Alias4ck +1 good job
    sujin190
        15
    sujin190  
       350 天前
    @liuxingdeyu #1 似乎不大可能做到吧,不是 python 语言自身难以做到,难的是那么多 C 开发的扩展都可能不兼容,这可就不是一件容易的事了
    liuxingdeyu
        16
    liuxingdeyu  
       350 天前
    @sujin190 我表达的问题,其实不是完全抛弃掉 gil ,是改变现在的 gil 。却是 pep703 说了 c api 会有的不少问题,所以我觉得真的实现了这件事的版本会是个大版本
    shalingye
        17
    shalingye  
       350 天前 via Android
    你所说的真正多线程,在 Python 里面应该叫做多进程,可以通过遍历列表等方式往进程池里面添加任务,同一时间执行的进程取内核数量,这样就可以充分利用 CPU 了。
    tc132
        18
    tc132  
       350 天前
    python 真的需要那么高的性能吗?你们是什么业务场景需要
    newmlp
        19
    newmlp  
       350 天前
    多进程也不是不可以
    RedisMasterNode
        20
    RedisMasterNode  
       350 天前
    之前一些实践基本是依赖 uwsgi 和 gunicorn 跑多个进程用的,承载压力的能力也不差
    sbldehanhan
        21
    sbldehanhan  
    OP
       350 天前
    @r6cb 挺不错的。不过 concurrent.futures 也支持多进程,既可以创建线程池,也可以创建进程池。
    wizardyhnr
        22
    wizardyhnr  
       350 天前
    楼上说的应该是 subinterpreter 。
    https://www.infoworld.com/article/3694512/python-312-faster-leaner-more-future-proof.html
    nogil 反对声音不小。就算技术上可行,其他人同意了,要是实现也是不知道什么时候了。
    这些开发者现在都很保守了,感觉很多人个人意愿只是小修小补混混了。
    julyclyde
        23
    julyclyde  
       337 天前
    concurrent 是 python 脚本写的啊,看看源码就知道原理了
    不会突破 python interpreter 的限制
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2722 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 05:27 · PVG 13:27 · LAX 22:27 · JFK 01:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.