Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a while loop as a wait in python

I have done this in C/C++ before where I have a while loop that acts as a wait holding the program up until the condition is broken. In Python I am trying to do the same with while(GPIO.input(24) != 0): and its says that it is expecting an indent. Is there anyway to get the script to hang on this statement until the condition is broken?

like image 994
Ziggy Avatar asked May 23 '13 18:05

Ziggy


2 Answers

Do note that an empty while loop will tend to hog resources, so if you don't mind decreasing the time resolution, you can include a sleep statement:

while (GPIO.input(24) != 0):
    time.sleep(0.1)

That uses less CPU cycles, while still checking the condition with reasonable frequency.

like image 127
Asad Saeeduddin Avatar answered Oct 25 '22 08:10

Asad Saeeduddin


In Python, you need to use the pass statement whenever you want an empty block.

while (GPIO.input(24) != 0):
    pass
like image 29
Platinum Azure Avatar answered Oct 25 '22 06:10

Platinum Azure