我需要一个帮助来停止getCPUtemperature():function和robot()函数。
def getCPUtemperature():
while True:
res = os.popen('vcgencmd measure_temp').readline()
temp1=int(float(res.replace("temp=","").replace("'C\n","")))
temp2= 9.0/5.0*temp1+32
print temp1,"C", "\n", temp2,"F"
time.sleep(0.5)
if __name__ == '__main__':
try:
#Thread(target = robot).start()
Thread(target = getCPUtemperature).start()
Thread(target = robot).start()
except KeyboardInterrupt:
# CTRL+C exit, turn off the drives and release the GPIO pins
print 'Terminated'
stop_movement()
raw_input("Turn the power off now, press ENTER to continue")
GPIO.cleanup()
quit()strong text
答案 0 :(得分:1)
让你的子线程守护进程并保持主线程处于活动状态以等待它们完成或按下ctrl-c。
尝试以下代码:
if __name__ == '__main__':
#Thread(target = robot).start()
t1 = Thread(target = getCPUtemperature)
t1.daemon = True # in daemon mode
t1.start()
t2 = Thread(target = robot)
t2.daemon = True
t2.start()
try:
while threading.active_count() > 1:
time.sleep(1)
except KeyboardInterrupt:
# CTRL+C exit, turn off the drives and release the GPIO pins
print 'Terminated'
stop_movement()
raw_input("Turn the power off now, press ENTER to continue")
GPIO.cleanup()
quit()