我在停止正在執行阻塞操作的線程時遇到問題。我書面方式使用通用產品安全指令的程序和它的Python綁定,該線程的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
問題是,下一個()方法被阻塞,因此,即使從我的主線程我打電話:
print "Stopping GPS thread"
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
當沒有GPS修復運行方法在next()上被阻止不會停止自己......任何想法?如果GPS已經修復,代碼工作正常。
非常感謝!
非常感謝。我想要一個與gpsd utc時間相比系統時間(python的日期時間)的tkinter時鐘,而且我真的希望保持系統時間以毫秒爲單位。我不想弄清楚如何使用Python線程,因爲我對Python很陌生(而且我一般使用的線程很少),這真的救了我。 +1,如果我可以的話。 – dylnmc