1

My python code runs a function that takes a long time:

Navigation() 

During this execution I'd like to have a reset button. For reset button to work, I used threading and I managed to get my code into threaded callback function with the button press. How do I terminate the Navigation() that is currently running and call another fresh Navigation() function? thanks.

Thanks in advance.

Jon Cairns
  • 11,783
  • 4
  • 39
  • 66
user2117336
  • 69
  • 1
  • 3
  • You need to post more code so others can see what you are talking about. For example, are you already using Thread, Thread.start(), etc... ? – Paul May 20 '13 at 15:11
  • I have Navigation() that runs in a while loop but each loop takes a long time. I need to terminate the function immediately in case there is a button press. – user2117336 May 20 '13 at 15:27
  • GPIO.add_event_detect(GPIO_RESET, GPIO.FALLING, callback=my_callback) This is how I receive interrupt now. – user2117336 May 20 '13 at 15:28

2 Answers2

0

If your navigation function has a loop that it executes, you could have a check to see if an "interrupt" variable is set to True. If so, you could have the Navigation function terminate during that check:

def navigation(self):
    # reset self.interrupt as appropriate
    while not self.interrupt:
        pass # Do something here
        # You will want some other exit condition as well, of course.

def button_pressed_response(self):
    self.interrupt = True
    self.navigation()

If you have access to a multithreaded library as you indicated, you could use a more elegant callback function and simplify the reset logic as well.

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
  • I do have a loop but each loop takes a long time to execute. I need to terminate the function immediately in case there is a button press – user2117336 May 20 '13 at 15:30
0

This answer pretty much sums up issues and pitfalls of terminating threads:

https://stackoverflow.com/a/325528

Community
  • 1
  • 1
Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58