简体   繁体   English

停止运行阻塞操作的线程

[英]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: 我正在编写一个使用gpsd及其python绑定的程序,该Thread的run方法如下所示:

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: 问题是next()方法正在阻塞,因此即使从我的主线程调用:

    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? 当没有GPS修复时,在next()上阻止运行方法,这将不会自行停止...有任何想法吗? If GPS has got a fix the code is working OK. 如果GPS已修复,则代码工作正常。

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: gps库具有一种非阻塞方法来检查数据是否可用,因此现在看起来像:

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. 它每5秒从gpsd读取GPS位置并将其写入文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM