Python Forum
Learn to use thread - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Learn to use thread (/thread-4210.html)



Learn to use thread - tony1812 - Jul-29-2017

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.


RE: Learn to use thread - DeaD_EyE - Jul-31-2017

You should start your thread: my_thread.start()