Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
time.sleep
#1
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:
Reply
#2
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
Reply
#3
Thank you for your kind reply. I will study your example.
Reply
#4
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?
Reply
#5
Quote:Can I use a call to 'sleep' in 1 thread without halting the execution of the other threads?
yes
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use subprocess.Popen and time.sleep chucky831 2 1,944 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 3,521 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Time.sleep: stop appending item to the list if time is early quest 0 1,871 Apr-13-2021, 11:44 AM
Last Post: quest
  Can you end the Time.sleep function boier96 9 9,433 Jan-16-2021, 10:09 PM
Last Post: Serafim
  kill thread or process asap, even during time.sleep nanok66 4 2,929 Apr-29-2020, 10:13 AM
Last Post: nanok66
  time.sleep works erratically, a bug in Python? stipcevic 2 3,882 Jan-21-2020, 09:38 PM
Last Post: Marbelous
  Adding Cosinus as utime.sleep variable Kreszy 0 1,709 Sep-19-2019, 04:27 PM
Last Post: Kreszy
  Why does time.sleep(wait) add time in python3 ? gerardg 4 3,389 Apr-28-2019, 06:26 PM
Last Post: gerardg
  How to achieve close to 1ms time.sleep kwekey 1 14,993 Mar-25-2019, 02:52 AM
Last Post: Larz60+
  If error sleep and then try again from there CaptainCsaba 2 2,687 Mar-01-2019, 06:28 AM
Last Post: CaptainCsaba

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020