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

关于 python 线程使用 CPU 核心的问题

  •  
  •   caduke · 2016-03-04 13:12:03 +08:00 · 4470 次点击
    这是一个创建于 2983 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请问 threading 能使用多个 cpu 吗?
    我写了一个简单的测试:
    对于_thread 模块:
    #!/usr/bin/python3.4
    import _thread as thread,time
    def a():
    i = 0
    while i < 10000000:
    time.time()
    i += 1
    for i in range(0,10):
    thread.start_new_thread(a,())
    time.sleep(5)


    对于 threading:
    import threading,time
    def a():
    i = 0
    while i < 10000:
    print(time.time())
    i += 1
    def main(num):
    thread_list = []
    for i in range(num):
    thread = threading.Thread(target=a,args=())
    thread_list.append(thread)
    for thread in thread_list:
    print(thread ,'starting...')
    thread.start()
    for thread in thread_list:
    thread.join()
    if __name__ == '__main__':
    main(20)

    我使用的是 centos6.7 的系统,在使用 top 命令查看时,都在使用 cpu0 和 cpu2 。
    但是我知道的是一个进程中的线程同一时刻只能有一个在运行,所以只能使用一个 CPU ,但是我看到结果却不一样,这是我测试的问题吗?
    11 条回复    2016-03-04 21:37:54 +08:00
    WKPlus
        1
    WKPlus  
       2016-03-04 13:39:50 +08:00
    如果单写一个 While 1 循环,多开几个线程, cpu 使用率可能更高,据说是 GIL 限制的是纯 python 代码的并发,因此并不是一个 python 进程 cpu 使用率不能超过 100%。

    具体哪些不是纯 python 代码,我也没查到,期待牛人解答。

    SOF 上有一个类似的问题: http://stackoverflow.com/questions/11615449/python-interpreters-uses-up-to-130-of-my-cpu-how-is-that-possible
    ryd994
        2
    ryd994  
       2016-03-04 13:44:55 +08:00
    @WKPlus GIL 是解释器的锁( I for interpreter )
    如果你写了个 C 模块,那么在 C 代码部分的计算量是可以不锁 GIL 的
    所以计算密集的核心部分可以试试写成 C 模块
    WKPlus
        3
    WKPlus  
       2016-03-04 13:50:19 +08:00
    @ryd994 但是为啥一个 While 1: pass 也可以让 cpu 使用率超过 100%呢?难道解析器把这个代码优化了?
    lebowsk1s
        4
    lebowsk1s  
       2016-03-04 14:06:07 +08:00 via Android
    计算密集型的代码开多线程没用的,得上多进程, GIL 这锅目前无解,只能用各种方法绕过
    nooper
        5
    nooper  
       2016-03-04 14:15:08 +08:00
    python mpi 解决。
    likuku
        6
    likuku  
       2016-03-04 14:24:32 +08:00
    python 多线程被 GIL 局限,只能使用一个 core ,

    一种解决办法是:任务对列化,按 CPU 核心数开多线程,每个多线程开 subprocess/独立子进程去执行任务。这样,可以用到所有 CPU 核心。
    likuku
        7
    likuku  
       2016-03-04 14:24:59 +08:00
    每个线程开一个 subprocess/独立子进程
    ethego
        8
    ethego  
       2016-03-04 14:42:39 +08:00
    @WKPlus while 1: pass 超过百分之百是因为超线程。
    virusdefender
        9
    virusdefender  
       2016-03-04 14:55:51 +08:00   ❤️ 1
    IO 密集型开多线程, CPU 密集型开多进程
    WKPlus
        10
    WKPlus  
       2016-03-04 15:57:39 +08:00
    @ethego 因为超线程?如果仅仅是因为超线程,是不是应该不会超过 200%( 1 物理核超线程之后变成 2 虚拟核)。但我试过 while 1:pass 开 10 个线程的话, cpu 使用率能达到 950%左右
    ryd994
        11
    ryd994  
       2016-03-04 21:37:54 +08:00
    @WKPlus while True 这个不需要解释器反复解释啊,就下去跑起来了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   823 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:43 · PVG 03:43 · LAX 12:43 · JFK 15:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.