[英]How to kill old thread and start new thread?
I'm just start learning about python and I have problem with my project to blink LED. 我刚刚开始学习python,而我的闪烁LED的项目有问题。 when I get new message and start new thread. 当我收到新消息并启动新线程时。 The old thread is still running. 旧线程仍在运行。 I want to kill old thread and start new thread. 我想杀死旧线程并启动新线程。 How to solve my problem? 如何解决我的问题? (Sorry if I'm not good in english but I'm trying) (对不起,如果我英语不好,但是我在努力)
def led_action(topic,message):
print topic+" "+message
if message == 'OFF':
#state = False
print ("Stoping...")
while message == 'OFF':
GPIO.output(8,GPIO.LOW)
elif message == 'ON':
#state = True
print ("Opening...")
while message == 'ON':
GPIO.output(8,GPIO.HIGH) #Set LED pin 8 to HIGH
time.sleep(1) #Delay 1 second
GPIO.output(8,GPIO.LOW) #Set LED pin 8 to LOW
time.sleep(1)
# Get message form NETPIE and Do something
def subscription(topic,message):
set = thread.start_new_thread(led_action, (topic,message))
def connection():
print "Now I am connected with netpie"
def disconnect():
print "disconnect is work"
microgear.setalias("test")
microgear.on_connect = connection
microgear.on_message = subscription
microgear.on_disconnect = disconnect
microgear.subscribe("/mails")
microgear.connect(True)
To terminate a python thread you need to exit your function. 要终止python线程,您需要退出函数。 You can do this by removing your while message == 'ON'/'OFF'
checks. 您可以通过删除while message == 'ON'/'OFF'
检查来做到这一点。 As message doesn't change anyways (it is passed to the function led_action
) those checks are unnecessary. 由于消息始终不变(传递给函数led_action
),因此不需要进行这些检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.