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
dzmcs
V2EX  ›  Python

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

  •  
  •   dzmcs · 2017-10-09 02:08:59 +08:00 via iPad · 2800 次点击
    这是一个创建于 2363 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    5 条回复    2017-10-09 23:09:13 +08:00
    scriptB0y
        1
    scriptB0y  
       2017-10-09 07:58:40 +08:00
    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  
       2017-10-09 10:45:22 +08:00
    @scriptB0y #1
    第一段的“ GIL 在解释器层面组织了真正的并行运行”,“组织”应为“阻止”吧
    scriptB0y
        3
    scriptB0y  
       2017-10-09 11:04:33 +08:00
    @halfcoder 是的 谢谢
    dzmcs
        4
    dzmcs  
    OP
       2017-10-09 15:07:06 +08:00
    @scriptB0y 谢谢!看了下 python 的线程应该是模拟的,threadmodule.c 中没有调用系统线程接口。书不错
    NoAnyLove
        5
    NoAnyLove  
       2017-10-09 23:09:13 +08:00
    @dzmcs 不要瞎说
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3623 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:36 · PVG 12:36 · LAX 21:36 · JFK 00:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.