Python Forum

Full Version: time.sleep
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Brand new to Python. If I want to pause the execution of my program for an hour, is sleep() the appropriate function to call? Do I have to express the value passed to sleep()in secs or can I use mins? I'm using a RPi 3B+ with a relay HAT. Thanks for helping.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

GPIO.setup(32,GPIO.OUT)     #Pin 32 = Relay #1 (Pump)  20 Min 1200 Sec
GPIO.output(32,GPIO.HIGH)   #HIGH = OFF

GPIO.setup(36,GPIO.OUT)     #Pin 36 = Relay #2(Solenoid A) 45 Min, 2700 Sec
GPIO.output(36,GPIO.HIGH)

GPIO.setup(38,GPIO.OUT)     #Pin 38 = Relay #3(Solenoid B) 45 Min
GPIO.output(38,GPIO.HIGH)

GPIO.setup(40,GPIO.OUT)     #Pin 40 = Relay #4(unused)
GPIO.output(40,GPIO.HIGH)

while True:
    try:
        GPIO.output(36,GPIO.LOW)    #Open Sol A
        GPIO.output(32,GPIO.LOW)    #Pump ON
        time.sleep(1200)            #Flood bed A for 20 min
        GPIO.output(38,GPIO.LOW)    #Open Sol B
        GPIO.output(36,GPIO.HIGH)   #Close Sol A
        time.sleep(1200)            #Flood bed B for 20 min
        GPIO.output(32,GPIO.HIGH)   #Pump OFF
        GPIO.output(38,GPIO.HIGH)   #Close Sol B
        time.sleep(1800)            #Allow for complete drainage of beds
    except KeyboardInterrupt:
sleep will pause for an hour, day or whatever if given the proper value.
It does not allow other processes take place (in same script) however.
A better way is to use an event which will create an event on timeout.
I have an example of this type of timer on this forum here: https://python-forum.io/Thread-Multi-thr...ight=timer
Thank you for your kind reply. I will study your example.
Multi-threaded-Timer-Class

In your example you define 4 functions, each running in their own thread. Can I use a call to 'sleep' in 1 thread without halting the execution of the other threads?
Quote:Can I use a call to 'sleep' in 1 thread without halting the execution of the other threads?
yes