I'm having problems stopping a thread which is executing a blocking operation. I'm writting a program that uses gpsd and it's python binding, the the Thread's run method looks like this:
def run(self):
print "inside run. outside while"
global gpsd
while self.running:
print "inside while"
try:
print "before next()"
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
print "after next()"
self.file_descriptor.write(str(int(time.time())) + ',' + str(gpsd.fix.latitude) + ',' + str(gpsd.fix.longitude) + ',' + str(gpsd.fix.altitude) + ',' + str(gpsd.fix.speed) + '\n')
print "after write"
#self.file_descriptor.write("self.running" + str(self.running) + '\n')
self.file_descriptor.flush()
print "after flush"
time.sleep(5)
print "after sleep"
except:
print "inside thread except"
raise
Problem is that the next() method is blocking, so even if from my main thread I call:
print "Stopping GPS thread"
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
when there is no GPS fix the run method is blocked on next() an not going to stop itself... any ideas? If GPS has got a fix the code is working OK.
Thanks a lot!