使用Python在单独线程中进行GPSd轮询
我正在开发一个python类,它表示一个空气质量站。每个空气质量监测站都有一个与之相关的全球定位系统,以便我们能够识别监测站的纬度和纵向坐标。GpsPoller在单独的线程上调用,并持续运行,直到线程终止。应使用GpsPoller在任何给定时间获取最新的GPS数据 以下是我迄今为止所做的工作:使用Python在单独线程中进行GPSd轮询,python,multithreading,gps,polling,gpsd,Python,Multithreading,Gps,Polling,Gpsd,我正在开发一个python类,它表示一个空气质量站。每个空气质量监测站都有一个与之相关的全球定位系统,以便我们能够识别监测站的纬度和纵向坐标。GpsPoller在单独的线程上调用,并持续运行,直到线程终止。应使用GpsPoller在任何给定时间获取最新的GPS数据 以下是我迄今为止所做的工作: class AirStation: def __init__(self): if not self._init_station(): raise Init
class AirStation:
def __init__(self):
if not self._init_station():
raise InitException("Could not initialize the AirStation!")
def _init_station(self):
self._id = utils.get_mac('wlan0')
self._gpsp = utils.GpsPoller()
self._gpsp.start() # start polling the GPS sensor
self._lat = self._gpsp.get_gps_data()
self._lon = self._gpsp.get_gps_data()
return True
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.session = gps(mode=WATCH_ENABLE)
self.current_value = None
self.running = True
def get_gps_data(self):
return self.current_value
def run(self):
while self.running:
self.current_value = self.session.next()
我对此代码有两个问题:
exit()
命令就会挂起,因为附加的线程会挂起我看了不少示例和文档,包括以下内容:
while True:
if self._lat is None:
self._lat = self._gpsp.get_gps_data()
elif self._lon is None:
self._lon = self._gpsp.get_gps_data()
else:
break
我相信我已经找到了解决以上问题的最佳方法。以下是我的想法: