0

I'm using a while loop in python:

def update_variable():
    global myVariable
    while True:
        myVariable = db['key']
        time.sleep(.5)

Now my question is: Does adding or removing the time.sleep() will change on how much cpu is consumed? Or it would change not to be using the cpu all the time?

P.S.: Its not a duplicate because I'm asking for the cpu that consumes the while loop, not the time.sleep()

  • A process that is sleeping on a regular timesharing OS yields the CPU for other processes to use. – Charles Duffy Jan 05 '21 at 01:39
  • The script won't consume any resources while sleeping. This means that the CPU utilization of a script containing sleep generally will be less than the same script without sleep. – mrconter1 Jan 05 '21 at 01:39
  • 2
    (re: postscript addendum) -- sure, you're asking about the loop as a whole, but the `time.sleep` makes your `while` loop yield the CPU. If you understand the linked questions' answers, I don't see how you couldn't reason out your own question's answer on that basis. – Charles Duffy Jan 05 '21 at 01:48
  • 2
    Also, if you follow the links and read the other questions themselves (and not just their titles), you'll see that they too (or at least the "long running processes" one) are about loops, not just about `sleep` on its own. – Charles Duffy Jan 05 '21 at 01:49

0 Answers0