thread.start_new_thread 不工作 [英] thread.start_new_thread not working

查看:183
本文介绍了thread.start_new_thread 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让这些 GPIO 端口同时打开和关闭,但在 RPi 上的间隔不同.我可以运行其中一个循环并且它可以工作,但是当我引入线程时它却没有.

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,())

推荐答案

这是因为没有主程序/循环.您的代码启动了这些线程,但随后它将结束并退出运行 python 的进程,从而终止线程.所以也许在底部添加一个 raw_input("Press enter to exit") .

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.

这篇关于thread.start_new_thread 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆