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

Python 是怎么知道一个线程是 io 操作,然后释放 GIL 锁给别的线程执行

  •  
  •   py1991 · Jan 15, 2019 · 3223 views
    This topic created in 2850 days ago, the information mentioned may be changed or developed.
    8 replies  •  2019-01-15 20:56:18 +08:00
    neoblackcap
    neoblackcap
    Jan 15, 2019
    1
    底层 IO 接口手动释放 GIL,这个其实你看看 CPython 源代码就知道了。
    py1991
    py1991
    OP
    Jan 15, 2019
    3
    @neoblackcap 惭愧,c 不会,看不懂=。=
    lihongjie0209
    lihongjie0209
    Jan 15, 2019
    4
    最简单的, Python 标准库中所有的 io 操作都记录一下, 那么你基于标准实现的其他库你可以就知道了.


    实在不行, 你还可以看看操作系统是不是有什么接口可以使用
    py1991
    py1991
    OP
    Jan 15, 2019
    5
    @ysc3839 do {
    Py_BEGIN_ALLOW_THREADS
    ret = fcntl(fd, code, buf);
    Py_END_ALLOW_THREADS
    } while (ret == -1 && errno == EINTR && !(async_err = PyErr_CheckSignals()));

    能否解释下,感觉只看懂了线程开始和结束,循环 fcntl 是否结束
    wwqgtxx
    wwqgtxx
    Jan 15, 2019
    6
    @py1991 Py_BEGIN_ALLOW_THREADS 这个宏的意思就是释放 GIL,然后 Py_END_ALLOW_THREADS 再锁上
    cyspy
    cyspy
    Jan 15, 2019
    7
    IO 的核心部分其实是系统内核完成的,调用内核的时候放开就行了
    zhichaozhang
    zhichaozhang
    Jan 15, 2019 via iPhone
    8
    线程执行是抢占式的 是由系统来调度的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2903 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 12:25 · PVG 20:25 · LAX 04:25 · JFK 07:25
    ♥ Do have faith in what you're doing.