vkumar vkumar - 14 days ago 9x
Python Question

Python time.sleep indefinitely

In Python's time module, there is a

function, where you can make Python wait x seconds before resuming the program. Is there a way to do this indefinitely until a condition is met? For example:

while True:
if x:


I am trying to make a pause function for my PyGame program. Any help is appreciated.


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.