Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`loop{}` versus `loop{sleep 1}`

I am using a loop to wait on a keyboard interrupt and then allow for some clean up operation before exit in a multi threaded environment.

begin
  loop {}
rescue Interrupt
  p "Ctr-C Pressed..Cleaning Up & Shutting Down"
  loop do
    break if exit_bool.false?
  end
  exit 130
end

This piece of code runs in the main thread. There are multiple threads performing several file and DB ops. exit_bool is an atomic var set by other threads to indicate they are in the middle of some operation. I check for the value and wait until it turns false and then exit.

I'm wondering what the cost of loop{} is as opposed to loop{sleep x}.

like image 246
a86 Avatar asked Dec 04 '15 09:12

a86


People also ask

What is time sleep 1 in Python?

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.

How do you wait 0.5 seconds in Python?

Python's time module contains many time-related functions, one of which is sleep() . In order to use sleep(), you need to import it. sleep() takes one argument: seconds. This is the amount of time (in seconds) that you want to delay your code.

How do you wait 1 second in Python?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.

What is the sleeping time of Python in hours?

Let the sleeping time and awake time of python be TS and TA respectively. Since the circle is divided into four quarters out of which three is shaded and each quarter contains six hours, Sleeping time of python is three times six, which is equal to 18 hours.

How does loop’s 6-hour prediction curve work?

The full prediction curve is shown on Loop’s main screen. If any part of the 6-hour prediction goes below the user’s suspend threshold, Loop will suspend basals. And, Loop will not add additional insulin, even if you are above targets, if your BG is predicted to be in range within 6 hours.

What is the difference between Control-IQ and loop?

This means, if you think that you want to add insulin to correct a stubborn high BG, you won’t find yourself automatically zero-basaled as a counter-balance like you often do in Loop. Control-IQ holds off on lowering basals for a longer period of time.

What is the difference between Medtronic loop and loop?

Loop mainly relies on the pump system itself to supply the alarms (like Medtronic Loop) or emulates a limited set of alarms (like Omnipod Loop). The alarms specific to Loop are loop-not-looping alarms at 20, 40, 60, and 120 minutes.

What happens when a thread sleep call is called?

It will actually cause the thread to stop executing (reduce the CPU usage of this thread to 0%) for the duration. The reason you see so heavy CPU usage is because your Thread.Sleep call has such a small time period you may as well not call it. Your loop will execute again almost straight away, causing the CPU load you see.


1 Answers

loop {} results in a high CPU utilization (~100%), whereas loop { sleep x } does not.

Another option is to just sleep forever:

begin
  sleep
rescue Interrupt
  # ...
end
like image 144
Stefan Avatar answered Oct 05 '22 19:10

Stefan