简体   繁体   中英

Stop/Kill Continuous Thread

I am attempting to continuously run a function that controls a stepper motor simultaneously while I plot incoming data. I can run the function but when I try to stop it with a keyboard interrupt the thread does not recognize it.

Here is the code:

              def forward():
               global delay
               while True:
                try:
                 cor = (-2.3*readadc(cor_adc)) + 1841
                 target = readadc(target_adc)
                 deltaP = target - cor
                 deltaP_list.append(deltaP)
                 if math.fabs((deltaP_list[-1] - deltaP_list[-2])) >= 10:
                  delay=(-.125628140704*math.fabs(deltaP)) + 30.1256281407
                 if delay < 5:
                  delay=5
                 print "delay", delay
                 print "diff", math.fabs((deltaP_list[-1] - deltaP_list[-2]))
                 print "Delta P", math.fabs(deltaP)
                 for i in range(0, int(50)):
                  setStep(1, 0, 0, 0)
                  time.sleep(int(delay) / 1000.0)
                  setStep(1, 1, 0, 0)
                  time.sleep(int(delay) / 1000.0)
                  setStep(0, 1, 0, 0)
                  time.sleep(int(delay) / 1000.0)
                  setStep(0, 1, 1, 0)
                  time.sleep(int(delay) / 1000.0)
                  setStep(0, 0, 1, 0)
                  time.sleep(int(delay) / 1000.0)
                  setStep(0, 0, 1, 1)
                  time.sleep(int(delay) / 1000.0)
                  setStep(0, 0, 0, 1)
                  time.sleep(int(delay) / 1000.0)
                  setStep(1, 0, 0, 1)
                  time.sleep(int(delay) / 1000.0)
                except KeyboardInterrupt:
                  print('end')
                  break
            Thread(target=forward).start()
            xAchse=pylab.arange(0,50,1)
            yAchse=pylab.array([0]*50)
            fig = pylab.figure(1)
            ax = fig.add_subplot(111)
            ax.grid(True)
            ax.set_title("Linear Algorithm")
            ax.set_xlabel("Time")
            ax.set_ylabel("mmHg")
            ax.axis([0,50,0,300])
            line1=ax.plot(xAchse,yAchse,'-')
            line2=ax.plot(xAchse,yAchse,'-')
            manager = pylab.get_current_fig_manager()

            def build_plot(arg):
              cor = (-2.3*readadc(cor_adc)) + 1841
              target = readadc(target_adc)
              cor_list.append(cor)
              target_list.append(target)
            def RealtimePloter(arg):
              CurrentXAxis=pylab.arange(0,50,1)
              line1[0].set_data(CurrentXAxis,pylab.array(cor_list[-50:]))
              line2[0].set_data(CurrentXAxis,pylab.array(target_list[-50:]))
              ax.axis([CurrentXAxis.min(),CurrentXAxis.max(),0, 300])
              manager.canvas.draw()
              #manager.show()
            timer = fig.canvas.new_timer(interval=1)
            timer.add_callback(RealtimePloter, ())
            timer2 = fig.canvas.new_timer(interval=1)
            timer2.add_callback(build_plot, ())               
            timer.start()
            timer2.start()
            pylab.show()

There's no time!

I believe the reason your thread instances won't listen to the interrupt is because it is super busy running the forward() method!

Try to see if appending:

import time #somewhere in your code
time.sleep(0)

at the end of your method, will make the threads listen! "Sleeping" zero seconds is equal to checking for signals.

Read more about it

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