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}
.
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.
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.
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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With