简体   繁体   中英

thread.start_new_thread not working

I'm attempting to make these GPIO ports turn on and off simultaneously, but at different intervals on a RPi. I can run one of the loops and it works but when I bring in the threading it does not.

import RPi.GPIO as GPIO  
from time import sleep  
import thread

GPIO.setmode(GPIO.BOARD)  

GPIO.setup(11, GPIO.OUT)  
GPIO.setup(13, GPIO.OUT)  
GPIO.setup(15, GPIO.OUT)  

def fast():  
    while True:  
        GPIO.output(11, True)  
        sleep(.02)  
        GPIO.output(11, False)  
        sleep(.02)  

def med():
    while True:
        GPIO.output(13, True)
        sleep(.2)
        GPIO.output(13, False)
        sleep(.2)

def slow():
    while True:
        GPIO.output(15, True)
        sleep(2)
        GPIO.output(15, False)
        sleep(2)

thread.start_new_thread(fast,())
thread.start_new_thread(med,())
thread.start_new_thread(slow,())

It's because there is no main program/loop. Your code starts those threads, but then it will come to the end of the code and exit the process running python, killing the threads. So maybe add a raw_input("Press enter to exit") at the bottom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM