0

I need a help to stop getCPUtemperature(): function together with robot() function.

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
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895

1 Answers1

1

make your child threads daemon and keep the main thread alive to waiting them finish or a ctrl-c pressed.

try the following code:

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()
hd1
  • 33,938
  • 5
  • 80
  • 91
Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28