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

python3 中,为什么把 checkinterval 设置的非常小没有让 CPU 密集型多线程变慢?

  •  
  •   jfry ·
    toaco · May 20, 2018 · 3689 views
  •   You need to sign in to view this topic
    This topic created in 3077 days ago, the information mentioned may be changed or developed.
    from threading import Thread
    
    def countdown(start, end):
        while end > start:
            end -= 1
    
    def single_thread(n):
        countdown(0, n)
    
    def multi_thread(n):
        t1 = Thread(target=countdown, args=(0, n // 2))
        t2 = Thread(target=countdown, args=(n // 2, n))
        t1.start()
        t2.start()
        t1.join()
        t2.join()
    
    if __name__ == '__main__':
        import timeit
        import sys
    
        sys.setswitchinterval(1)
        print(timeit.timeit("""import gil;gil.multi_thread(10000000);""", number=1))
        # 1.07s
        sys.setswitchinterval(0.001)
        print(timeit.timeit("""import gil;gil.multi_thread(10000000);""", number=1))
        # 1.09s
    

    按照我的理解,将切换间隔设置的更小,导致更多次的线程睡眠 /唤醒操作,然后总的执行时间应该变长,但是这里并没有,谁能告诉我是为什么吗? 测试环境为:四核 Ubuntu python3.5

    4 replies  •  2018-05-22 09:53:24 +08:00
    enenaaa
        1
    enenaaa  
       May 20, 2018
    第一, 线程切换时间依赖于操作系统。
    第二, 这个值只是解释器估计的理想时间。在执行较长的函数内并不会主动中断。因为解释器没有线程调度功能, 需要依赖系统。
    jfry
        3
    jfry  
    OP
       May 21, 2018
    @enenaaa @chenxytw 我是看了一个讲 Python3 GIL 的 PDF 之后做的这个实验,根据这个 PDF 中的描述,当两个线程 A,B 同时运行的时候,假如 A 正在运行,此时 B 会阻塞直到超时,然后设置 gil_drop_request 为 1,A 检测到之后就会释放 GIL 并且通过信号唤醒 B,因此我理解的是此时操作系统就会让 A 睡眠,然后调度 B .是我理解错了吗?
    PDF 地址: http://www.dabeaz.com/python/NewGIL.pdf
    enenaaa
        4
    enenaaa  
       May 22, 2018 via Android
    @jfry 线程切换的条件一是当前线程主动挂起,二是当前时间片用完。这两个条件都由系统决定,解释器是在系统切换完之后加锁。setswitchinterval 只是在解释器加锁时的一个参照时间,而不是系统的时间片时间。

    这个程序里线程一直在运行,用满系统时间片。导致解释器无从按指定参数去控制。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3124 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 04:33 · PVG 12:33 · LAX 21:33 · JFK 00:33
    ♥ Do have faith in what you're doing.