推荐学习书目
› 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 · May 5, 2023 · 5223 views
    This topic created in 1281 days ago, the information mentioned may be changed or developed.

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

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

    python 的多线程就是 pthread

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

    但是这个主要是卡纯 python 的

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

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

    但是因为早年 linux 没有线程, 之后有段时间线程也是基于 fork 的,2.6 的时候又大改过一次, 所以用的人并不多, 导致主流的 Linux 下面都提倡 IO 异步并发, 计算的直接 fork, 用线程的并不多.....
    matrix1010
    matrix1010
    May 5, 2023
    5
    @liuxingdeyu 你这是大新闻, 没有 GIL 的 Python 可以算作另一种语言了
    sbldehanhan
    sbldehanhan
    OP
    May 5, 2023
    6
    @githmb 但是如果线程的那个函数里面又调用了其他 Python 函数,那被调用的函数也得用 C 实现一遍。这样工作量就太大了。
    sbldehanhan
    sbldehanhan
    OP
    May 5, 2023
    7
    @liuxingdeyu 为了快,现在太慢了。
    noahliaszn
    noahliaszn
    May 5, 2023
    9
    @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
    liuxingdeyu
    May 5, 2023
    10
    @matrix1010 @Alias4ck 额,是我表达有误了,我想表达的就是 3.12 的这个改版,去掉了很多关于核心使用的限制
    ihciah
    ihciah
    May 5, 2023 via iPhone
    11
    换 Mojo
    gitopen
    gitopen
    May 5, 2023
    12
    @r6cb ➕1
    hertzry
    hertzry
    May 5, 2023
    13
    multiprocessing 凑活能用。
    pollux
    pollux
    May 5, 2023
    14
    @Alias4ck +1 good job
    sujin190
    sujin190
    May 5, 2023
    15
    @liuxingdeyu #1 似乎不大可能做到吧,不是 python 语言自身难以做到,难的是那么多 C 开发的扩展都可能不兼容,这可就不是一件容易的事了
    liuxingdeyu
    liuxingdeyu
    May 5, 2023
    16
    @sujin190 我表达的问题,其实不是完全抛弃掉 gil ,是改变现在的 gil 。却是 pep703 说了 c api 会有的不少问题,所以我觉得真的实现了这件事的版本会是个大版本
    shalingye
    shalingye
    May 5, 2023 via Android
    17
    你所说的真正多线程,在 Python 里面应该叫做多进程,可以通过遍历列表等方式往进程池里面添加任务,同一时间执行的进程取内核数量,这样就可以充分利用 CPU 了。
    tc132
    tc132
    May 5, 2023
    18
    python 真的需要那么高的性能吗?你们是什么业务场景需要
    newmlp
    newmlp
    May 5, 2023
    19
    多进程也不是不可以
    RedisMasterNode
    RedisMasterNode
    May 5, 2023
    20
    之前一些实践基本是依赖 uwsgi 和 gunicorn 跑多个进程用的,承载压力的能力也不差
    sbldehanhan
    sbldehanhan
    OP
    May 5, 2023
    21
    @r6cb 挺不错的。不过 concurrent.futures 也支持多进程,既可以创建线程池,也可以创建进程池。
    wizardyhnr
    wizardyhnr
    May 5, 2023
    22
    楼上说的应该是 subinterpreter 。
    https://www.infoworld.com/article/3694512/python-312-faster-leaner-more-future-proof.html
    nogil 反对声音不小。就算技术上可行,其他人同意了,要是实现也是不知道什么时候了。
    这些开发者现在都很保守了,感觉很多人个人意愿只是小修小补混混了。
    julyclyde
    julyclyde
    May 18, 2023
    23
    concurrent 是 python 脚本写的啊,看看源码就知道原理了
    不会突破 python interpreter 的限制
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4619 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 103ms · UTC 05:46 · PVG 13:46 · LAX 21:46 · JFK 00:46
    ♥ Do have faith in what you're doing.