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


    实在不行, 你还可以看看操作系统是不是有什么接口可以使用
    py1991
        5
    py1991  
    OP
       Jan 15, 2019
    @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
        6
    wwqgtxx  
       Jan 15, 2019
    @py1991 Py_BEGIN_ALLOW_THREADS 这个宏的意思就是释放 GIL,然后 Py_END_ALLOW_THREADS 再锁上
    cyspy
        7
    cyspy  
       Jan 15, 2019
    IO 的核心部分其实是系统内核完成的,调用内核的时候放开就行了
    zhichaozhang
        8
    zhichaozhang  
       Jan 15, 2019 via iPhone
    线程执行是抢占式的 是由系统来调度的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   758 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 20:26 · PVG 04:26 · LAX 13:26 · JFK 16:26
    ♥ Do have faith in what you're doing.