简体   繁体   中英

while True system resources effect

Why does while True: not affect CPU too much in some cases and in other does? 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. 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. Why in the second example I get 50% load and nearly nothing on the first one? Is it because of the 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?

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

threading.enumerate() does not affect the current process CPU because of the way it's measured.

But it still polls system resources continuously so even if your process doesn't show high CPU usage, the system is still stressed. 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. 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.

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. 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. It is more like a visual bug, similar to the windows idle process and some other processes. 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. 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.

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.

funny to see this bug I learned whe nI was like 12 learning C pop up and all the 'dumb' answers given.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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