简体   繁体   中英

how to break a thread function in python

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

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

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