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!