简体   繁体   中英

Stop a thread running a blocking operation

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!

Ok, I think I did it. The gps library has a non-blocking method to check if data is available, so now it looks like:

def run(self):
    global gpsd
    while self.running:
        try:
            if gpsd.waiting(): #only True if data is available
                gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer

            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')
            self.file_descriptor.flush()
            time.sleep(5)
        except:
            raise

And it's working properly. Thanks!

I know it's not very elegant, but this is the code I've come to so far, and seems to be working OK. Just in case is useful for anyone.

It's reading the GPS position from gpsd and writing it to a file every 5 seconds.

while self.running:
                try:
                    if gpsd.waiting():
                        #print "inside waiting()"
                        while gpsd.waiting():
                            gpsd.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')
                        self.clear_fix()                
                    else:                    
                        self.file_descriptor.write(str(int(time.time())) + ",NO_GPS_FIX\n")

                    self.file_descriptor.flush()
                    time.sleep(5)
                except:
                    print "Exception on run()inside thread"
                    raise

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