推荐学习书目
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
dzmcs
V2EX  ›  Python

Python 的线程是怎么实现的?并发差在哪里?

  •  
  •   dzmcs · Oct 9, 2017 via iPad · 3780 views
    This topic created in 3137 days ago, the information mentioned may be changed or developed.

    线程是虚拟的还是调用系统 api,进入 os 调度队列的?

    都说 python 并发差,全局解释锁什么的,multiprocessing 库是否调用了系统 API,进程有木有进入 os 调度队列?到底差在哪里?

    5 replies    2017-10-09 23:09:13 +08:00
    scriptB0y
        1
    scriptB0y  
       Oct 9, 2017
    GIL 是保证每一个时间内 CPython 只有一个线程在运行,你看 Java 需要关心集合什么的是不是线程安全的,用 CPython 基本可以不管,因为 CPython 同时运行的线程只有一个。

    所以 CPython 的线程没有真正的并行执行,例如要用 4 个 CPU 执行计算任务,用 CPython 的线程是不可能的。

    最近翻的一本书有一节讲了 GIL 的瓶颈: http://python-parallel-programmning-cookbook.readthedocs.io/zh_CN/latest/chapter2/13_Evaluating_the_performance_of_multithread_applications.html
    halfcoder
        2
    halfcoder  
       Oct 9, 2017
    @scriptB0y #1
    第一段的“ GIL 在解释器层面组织了真正的并行运行”,“组织”应为“阻止”吧
    scriptB0y
        3
    scriptB0y  
       Oct 9, 2017
    @halfcoder 是的 谢谢
    dzmcs
        4
    dzmcs  
    OP
       Oct 9, 2017
    @scriptB0y 谢谢!看了下 python 的线程应该是模拟的,threadmodule.c 中没有调用系统线程接口。书不错
    NoAnyLove
        5
    NoAnyLove  
       Oct 9, 2017
    @dzmcs 不要瞎说
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5589 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 09:05 · PVG 17:05 · LAX 02:05 · JFK 05:05
    ♥ Do have faith in what you're doing.