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

Python 死循环为什么能跑满 CPU?求解

  •  
  •   uhayate · Oct 13, 2017 · 9322 views
    This topic created in 3142 days ago, the information mentioned may be changed or developed.
    def dead_loop():
        while True:
            pass
    
    
    dead_loop()
    

    20171013150785976921302.png
    在双核的 macbook pro 上,活动监视器显示跑满 cpu。 不应该只能利用单核达到 50%么

    16 replies    2017-10-13 15:40:52 +08:00
    arischow
        1
    arischow  
       Oct 13, 2017 via iPhone   ❤️ 1
    这应该就是单核的比例
    momo1999
        2
    momo1999  
       Oct 13, 2017 via Android   ❤️ 1
    天啦,我的程序可以跑 400%
    RqPS6rhmP3Nyn3Tm
        3
    RqPS6rhmP3Nyn3Tm  
       Oct 13, 2017 via iPhone
    显然是单核的比例,我这儿 top 经常 400%
    uhayate
        4
    uhayate  
    OP
       Oct 13, 2017
    @arischow 好像就是这样的。我看了一下是逻辑 cpu 是 4 核,开 4 个程序就 400%
    uhayate
        5
    uhayate  
    OP
       Oct 13, 2017 via iPhone
    @BXIA 嗯嗯
    coltguo
        6
    coltguo  
       Oct 13, 2017
    哎,其实还好
    CreSim
        7
    CreSim  
       Oct 13, 2017 via Android
    Unix 衍生系统的负载:1=一个核满载。。
    我的路由器平均负载都是 1.x 也就是 100+%
    simpleapples
        8
    simpleapples  
       Oct 13, 2017
    activity monitor 中的 100%是单核的比例 再开一个 python 跑 while true 就是将近 200%了
    scriptB0y
        9
    scriptB0y  
       Oct 13, 2017
    @simpleapples 楼上真像,不然 monitor 里面加起来超过 100%了
    azh7138m
        10
    azh7138m  
       Oct 13, 2017
    @CreSim 也不全是,安卓日常 20+
    Sapp
        11
    Sapp  
       Oct 13, 2017 via Android
    这个占用跑到 100 多都有的,得看下面那个占用。
    opengps
        12
    opengps  
       Oct 13, 2017
    所有语言的死循环都可以跑满 cpu,循环就是消耗计算,靠 cpu 完成的
    est
        13
    est  
       Oct 13, 2017
    谁贴一个纯 CPython 代码跑满所有 CPU 的我给他加分!
    ToughGuy
        14
    ToughGuy  
       Oct 13, 2017   ❤️ 1
    @est

    import multiprocessing as mp
    from concurrent.futures import ProcessPoolExecutor

    def fn():
    while 1:
    pass

    with ProcessPoolExecutor() as executor:
    for i in range(mp.cpu_count()):
    executor.submit(fn)





    对齐我懒得弄了。
    est
        15
    est  
       Oct 13, 2017   ❤️ 1
    @ToughGuy 不错。不过单进程能实现不 (滑稽脸)
    ToughGuy
        16
    ToughGuy  
       Oct 13, 2017
    @est

    这很无聊额。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3694 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 10:24 · PVG 18:24 · LAX 03:24 · JFK 06:24
    ♥ Do have faith in what you're doing.