0

Is it possible to end a thread whos target function is running an endless loop?

Background:

I'm trying to develop a Python software for a solar tracker. The software is running on a Raspberry Pi 3. The programm has to be able to start the tracking and also to end it, if desired. I use threads for tracking altitude and azimuth indepentend from each other, and to keep a gui running while the tracking is running.

The Code looks about:

def track_azimuth():
    while True:
        # querying azimuth using pysolar
        # drive a stepper motor according to azimuth using RPi.GPIO

def track_altitude():
   while True:
       # querying altitude using pysolar
       # drive another stepper motor according to altitude using RPi.GPIO

# bound to GUI-Button "START"
def start_tracking():
    thr_azi = Thread(target = track_azimuth)
    thr_alti = Thread(target = track_altitude)
    thr_azi.start()
    thr_alti.start()

# bound to GUI-Button "STOP"
def stop_tracking():
    thr_azi.end()   # I'm aware this method doesn't exist
    thr_alti.end()   # the same

I'm aware that the standard threading module doesn't support an .end() method. But it's possible to implement an own stoppable thread like in:

Is there any way to kill a Thread?

I have tried it, but it didn't work for me. I have also tried processes and to end them using the .terminate() method from the multiprocessing module. It didn't work too.

Actual result:

Threads/processes allways keep on running, and so do the stepper motors :(

Only closing the python shell works.

Expected result:

Stepper motors stopping when the "STOP" Button is clicked.

Any ideas on how to solve my problem? Thank you in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andre
  • 321
  • 1
  • 12
  • The Is there a way to kill a Thread really has the answer. How did you try to implement that approach? – g_bor Sep 26 '20 at 11:01
  • @g_bor: I have tried the first mentioned approach in "Is there any way to kill a Thread": the StoppableThread class. And than I have used the .stop() method in my stop_tracking() function - but it still didn't work – Andre Sep 26 '20 at 12:51
  • The while true loops have to be changed to something like: while thread_is_not_stopping, where that functions checks on a shared variable that marks the stopping. – g_bor Sep 26 '20 at 20:44
  • 1
    I have found a solution like the first one in [link](https://stackoverflow.com/questions/18018033/how-to-stop-a-looping-thread-in-python) I didn't even had to create an own Thread class. In the target function it has to check whether the thread executing this function is still alive with _getattr(threading.currentThread, is_running, True)_ – Andre Sep 29 '20 at 06:48

1 Answers1

0

In most situations, you can't "kill" a thread in your application. You can however set a flag in the thread, asking it politely to point a gun at its own head and pull the trigger, and well-programmed threads will do just that.

Killing a thread - at any time - would likely create huge stability problems, for example if mutexes etc. or other resources like memory, open files, are not destroyed properly.

gnasher729
  • 51,477
  • 5
  • 75
  • 98