Python Forum

Full Version: Learn to use thread
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am experiment with thread on the Pi. I want to keep the main thread for the UI. Start a new threat to turn a relay on and off. The part for the relay tread is:
import threading, time 
#from tkinter import *
from time import sleep
#Prepare GPIO
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) #disable annoying warning messages
GPIO.setup(40,GPIO.OUT)
#initially is off
GPIO.output(40,GPIO.LOW)

def foo():
  print ("Hello threading!")
  
def timed_on_off():
	GPIO.output(40,GPIO.HIGH)
	sleep(4)
	GPIO.output()
	sleep(4)

my_thread = threading.Thread(target = timed_on_off)
When I run it in Thonny, it doean't give me any error but neighter functions foo() nor timed_on_off() does anything. What am I doing wrong ? Thanks.
You should start your thread: my_thread.start()