diff options
author | David Raeman <david@synopticengineering.com> | 2022-02-23 04:37:48 +0000 |
---|---|---|
committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2022-03-23 16:19:04 -0500 |
commit | e57d22d525d93a72f9bc99fa1359aad0c51c921e (patch) | |
tree | 77c5d43b7c61b05fc8acd2851bef35d1dad2d4a2 | |
parent | 8890e77dffffec59d53816d260c5a94600471acb (diff) | |
download | uhd-e57d22d525d93a72f9bc99fa1359aad0c51c921e.tar.gz uhd-e57d22d525d93a72f9bc99fa1359aad0c51c921e.tar.bz2 uhd-e57d22d525d93a72f9bc99fa1359aad0c51c921e.zip |
mpm: speed up reading gps mboard sensors
This commit uses a more performant buffered I/O approach for reading the
gpsd socket. Previously, querying a gps mboard sensor on an mpm radio
would take 300-500ms due to the loop reading one byte at a time.
-rw-r--r-- | mpm/python/usrp_mpm/gpsd_iface.py | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/mpm/python/usrp_mpm/gpsd_iface.py b/mpm/python/usrp_mpm/gpsd_iface.py index 8d6b7049a..2c9c40561 100644 --- a/mpm/python/usrp_mpm/gpsd_iface.py +++ b/mpm/python/usrp_mpm/gpsd_iface.py @@ -129,6 +129,7 @@ class GPSDIface: self.log = get_main_logger('GPSDIface') # Make a socket to connect to GPSD self.gpsd_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.gpsd_sockfile = self.gpsd_socket.makefile(encoding='ascii') def __enter__(self): self.open() @@ -187,23 +188,18 @@ class GPSDIface: query_cmd = b'?WATCH={"enable":false};' self.gpsd_socket.sendall(query_cmd) - def socket_read_line(self, timeout=60, interval=0): + def socket_read_line(self, timeout=60): """ Read from a socket until newline. If there was no newline until the timeout occurs, raise an error. Otherwise, return the line. """ - line = b'' - end_time = time.time() + timeout - while time.time() < end_time: - socket_ready = select.select([self.gpsd_socket], [], [], 0)[0] - if socket_ready: - next_char = self.gpsd_socket.recv(1) - if next_char == b'\n': - return line.decode('ascii') - line += next_char - else: - time.sleep(interval) - raise socket.timeout + old_timeout = self.gpsd_socket.gettimeout() + self.gpsd_socket.settimeout(timeout) + try: + # get a file interface to socket, read until newline, and trim it + return self.gpsd_sockfile.readline().strip() + finally: + self.gpsd_socket.settimeout(old_timeout) def read_class(self, class_name, socket_timeout=60): """return json data for spcecfic key of 'class' |