There is nothing inherently wrong in this pattern. I have used the daemon function in init.d to start a very similar python script. As long as "do something" doesn't leak, it should be able to run forever.
Answer from stark on Stack OverflowThere is nothing inherently wrong in this pattern. I have used the daemon function in init.d to start a very similar python script. As long as "do something" doesn't leak, it should be able to run forever.
I think that either way
time.sleep()
will not stop the recursion limit Because sleep only pauses the execution , and doesn't free any kind of memory check https://docs.python.org/2/library/time.html the Time.sleep() description
It suspends the operation , but it will not do any memory optimization
Always add some form of sleep in while True loops
python - Infinite loop with setting time of sleep - Stack Overflow
python - Sleep() function inside a infinite while loop - Stack Overflow
Efficient and fast Python While loop while using sleep() - Stack Overflow
What causes an infinite loop in a Python program?
What are the consequences of an infinite loop in Python programs?
How can I detect an infinite loop in my Python program?
My computer gets really hot when I use python sometimes, and I think a while True loop would really put a lot of stress on it. Will putting time.sleep() in the while True loop result in the CPU having a bit of an easier time or not really?
The slow CPU wasting part is the "do serial sending". The while loop with just a short sleep will use negligible CPU.
Can you show the serial sending code. There may be a way to speed that up.
On this rather slow CPU I see this:
import time
while True: time.sleep(0.2) # 0% CPU
while True: time.sleep(0.02) # 0% CPU
while True: time.sleep(0.002) # 0.5% CPU
while True: time.sleep(0.0002) # 6% CPU
while True: time.sleep(0.00002) # 18% CPU
Now do some extra work in the loop:
import time
while True: range(10000) and None; time.sleep(0.2) # 1% CPU
while True: range(10000) and None; time.sleep(0.02) # 15% CPU
while True: range(10000) and None; time.sleep(0.002) # 60% CPU
while True: range(10000) and None; time.sleep(0.0002) # 86% CPU
I ran those in the interpreter and stopped each while loop with ctrl-C.
In regards to your comment on Joachim's answer:
Then your microcontroller code needs a redesign. Otherwise you're just turning you general-purpose computer into nothing more than a dumb "microcontroller" that iterates over unneeded code repeatedly, hence the 100% cpu. Another symptom of you using your computer incorrectly is the fact that your hardware motor's speed depends on the speed at which you send commands to it via the serial interface. You need to "command" it with the computer which will host your high-level logic. And your micro-controller needs to handle the low-level, repetative control of the motor.
Keep track of the next time you want to print the time.
def main():
next_time = datetime.datetime.now()
delta = datetime.timedelta(seconds=30)
while True:
period = datetime.datetime.now()
# do something here
if period >= next_time:
# every 30 seconds do something here 1 time, like saving in database
print(period)
next_time += delta
# Or, depending on how long the main body took
# and when you want to do this again,
# next_time = period + delta
Keep track of the last time you printed and print a new time only if it's more than one second after the last time.
lastTime = datetime.datetime.now()
while True:
period = datetime.datetime.now()
if period.second % 30 == 0 and (period - lastTime).total_seconds() >= 1:
print(period)
lastTime = period
Like Tigerhawk mentions in their comment, this isn't a great way of doing it though, because you're tying up a CPU core to do these iterations when it could be doing something else.
Something like this:
while not x: time.sleep(0.1)
will wait until x is true, sleeping a tenth of a second between checks. This is usually short enough for your script to seem to react instantly (in human terms) when x becomes true. You could use 0.01 instead if this is not quick enough. In my experience, today's computers are fast enough that checking a simple condition even every hundredth of a second doesn't really make a dent in CPU usage.
Of course, x should be something that can actually change, e.g. a function call.
Your code in the question implies that you want some other thread to resume your program. In that case you could use resumed = threading.Event(). You could create it in one thread and pass it into another:
while not resumed.wait(): # wait until resumed
"continue waiting"
Call resumed.set() to resume this code immediately.
I am trying to make a pause function for my PyGame program. Any help is appreciated.
Use pygame.time. Typically, you have the main loop where you update the state of the game and at the end of the loop you call clock.tick(60) # 60 fps. It is enough to use paused flag in this case, to skip the updating.