简体   繁体   English

而真正的系统资源影响

[英]while True system resources effect

Why does while True: not affect CPU too much in some cases and in other does? 为什么while True:在某些情况下不会影响CPU太多而在其他情况下呢? How does one prevent that? 如何防止这种情况?

I wrote a simple program and was worried that 我写了一个简单的程序,并担心

import threading
import time

#sleeptime = 0.1
while True:
    #time.sleep(sleeptime)
    if not [thread.name for thread in threading.enumerate() if thread.name == 'control']:
        threading.Thread(target=control, name='control').start()

would consume all of computational resources (or at least the cap given by OS) for checking the if statement (most of the time it's false so it should loop quite rapidly) so I initially put system.sleep(sleeptime) there to avoid that but I was surprised when I removed it it had no measurable effect on system resources. 将消耗所有计算资源(或至少由OS给出的上限)来检查if语句(大多数情况下它是假的,所以它应该非常快地循环)所以我最初把system.sleep(sleeptime)放在那里以避免当我删除它时,我很惊讶它对系统资源没有可测量的影响。 Why? 为什么?

That brought me to the question how often does the system check the condition. 这让我想到了系统检查条件的频率。

Naively: If i do something like 天真:如果我做了类似的事情

import time
i=0
before = time.process_time()
while True:
    i += 1
    if i >= 100000000: #1e8 but writing 1e8 adds ~ 30% time
        break
after = time.process_time()
print(after-before)

on my machine I get 11 seconds and 50% CPU load while running. 在我的机器上运行时,我得到11秒和50%的CPU负载。 Why in the second example I get 50% load and nearly nothing on the first one? 为什么在第二个例子中我得到50%的负载而在第一个例子中几乎没有? Is it because of the i += 1 ? 是因为i += 1

Can one profile the while loop (how often it loops and how much resources it consumes) without slowing it down or causing overhead and hence making the measured value irrelevant? 是否可以对while循环进行分析(循环次数和消耗的资源量),而不会减慢速度或导致开销,从而使测量值无关紧要?

I am doing this on Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32 我在win32上的Python 3.6.2(v3.6.2:5fd33b5,2017年7月8日,04:57:36)[MSC v.1900 64位(AMD64)]上这样做

threading.enumerate() does not affect the current process CPU because of the way it's measured. threading.enumerate()不会影响当前进程 CPU,因为它的测量方式。

But it still polls system resources continuously so even if your process doesn't show high CPU usage, the system is still stressed. 但它仍然会持续轮询系统资源,因此即使您的进程没有显示高CPU使用率,系统仍然会受到压力。 It's just that it isn't measured because it's not done within your process execution context, but in the system execution context. 它只是因为它没有在流程执行上下文中完成,而是在系统执行上下文中完成。

I suggest that you apply a simple time to your process, which, on Linux, is supposed to print the user & system time. 我建议您在流程中应用一个简单的time ,在Linux上,应该打印用户和系统时间。 I'm convinced that you'll get a lot of system time in the first example (and none in the second) 我相信你会在第一个例子中获得大量的系统时间(并且在第二个例子中没有)

Your second example just tells python to perform a CPU intensive loop (because of i+=1 and your test below, but pass would do the same, except it would be infinite), without system calls, it hogs the CPU all right and it is measured properly by the system. 你的第二个例子告诉python执行一个CPU密集型循环(因为i+=1并且你的测试在下面,但是pass会做同样的事情,除非它是无限的),没有系统调用,它会占用CPU并且它是由系统正确测量。

So in the first case, unless you want a very high reactivity, and you're not running that for too long, I'd suggest that you add a tiny delay between the tests, so the system can have a little break. 所以在第一种情况下,除非你想要一个非常高的反应性,并且你没有运行那么长时间,我建议你在测试之间加一点延迟,这样系统就可以稍微休息一下。

Lots of dumb answers from people who read books and only wasted time in schools, not as many direct logic or answers I see. 读书的人很多愚蠢的答案,只在学校浪费时间,而不是我看到的那么多直接的逻辑或答案。

while(true) will set your program to use all the CPU power that's basically 'alloted' to it by the windows algorithms to run what is in the loop, usually as-fast-as-possible over and over. while(true)会将程序设置为使用Windows算法基本上“分配”给它的所有CPU功率来运行循环中的内容,通常尽可能快地反复执行。 This doesn't mean if it says 100% on your application, that if you run a game, your empty loop .exe will be taking all your OS CPU power, the game should still run as intended. 这并不意味着如果它对您的应用程序说100%,那么如果您运行游戏,您的空循环.exe将占用您所有的操作系​​统CPU能力,游戏仍应按预期运行。 It is more like a visual bug, similar to the windows idle process and some other processes. 它更像是一个视觉错误,类似于Windows空闲进程和其他一些进程。 The fix is to add a Sleep(1) (at least 1 millisecond) or better yet a Sleep(5) to make sure other stuff can run and ensure the CPU is not constantly looping your while(true) as fast as possible. 修复是添加一个Sleep(1)(至少1毫秒)或更好的睡眠(5)以确保其他东西可以运行并确保CPU不会一直尽可能快地循环你的while(true)。 This will generally drop CPU usage to 0% or 1% in the visual queue as 1 full millisecond is a big resting time for even older CPU. 这通常会在可视队列中将CPU使用率降低到0%或1%,因为即使是较旧的CPU,1毫秒也是一个很长的休息时间。

Many times while(trues) or generic endless loops are bad designs and can be drastically slowed down to even Sleep(1000) - 1 second interval checks or higher. 很多时候(真实)或通用的无限循环都是糟糕的设计,并且可以大幅减慢甚至睡眠(1000) - 1秒间隔检查或更高。

funny to see this bug I learned whe nI was like 12 learning C pop up and all the 'dumb' answers given. 有趣的是看到这个bug我学到了什么,我就像12学习C弹出一样,给出了所有“愚蠢”的答案。

Just know if you try it, unless the scripted slower language you have learned to use has fixed it somewhere along the line by itself, windows will claim to use a lot of CPU on doing an empty loop when the OS is actually having free resources to spend. 只要知道你是否尝试过它,除非你学会使用的脚本化的较慢的语言已经将它自己固定在某个地方,当操作系统实际上有免费的资源时,windows会声称使用大量的CPU进行空循环。花。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM