gpsd-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gpsd-users] Flush unconsumed GPS reports in Python


From: Mike Tubby
Subject: Re: [gpsd-users] Flush unconsumed GPS reports in Python
Date: Wed, 20 Dec 2017 15:23:12 +0000
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0

Alex,

I've not written in Python but can't help thinking you've got this sort of inside out...?

Can you not write your code so that it is 'event driven' by GPS, i.e. read from GPSD an 'upcalls' to whatever needs the GPS data?  Or, alternatively, if you don't want it that tightly coupled - upcall to a routine that stores (buffers) the most recent GPS data so that when another part of your code needs it you just fetch it from your local buffer - rather than having to play catch-up with a backed up buffer, discarding messages, etc. - the latter approach being classic double-buffering.

Mike


On 12/20/2017 10:16 AM, Alex wrote:
Hi Gary, thank you for the reply.

I understand the logic of what you say and it was my intention to implement something like that. However, given that this is probably a common scenario - I was wondering if I wouldn't be reinventing the bicycle, and if there are existing solutions to this problem.


I am guessing there are none, so I wrote this, with the intention of calling this function every time I need a fresh report. It clears the queue, then I can call my regular read function:

def flush_gps(self):
    '''Clear every unread report in the GPS buffer, by reading
    everything that is there, until we get a StopIteration
    exception'''
    logger.debug('Starting GPS report flushing')
    unconsumed = 0
    while True:
        try:
            self.gps_session.next()
            unconsumed += 1
        except StopIteration:
            break
    logger.debug('GPS flushed unconsumed %i reports', unconsumed)



This doesn't give me the expected result though. I see `Starting GPS report flushing` in the log, but it seems that the StopIteration exception is never raised, so the loop never breaks.

1. Is it possible that the speed at which new reports are received is much higher than the speed at which they are consumed?  (even without any `sleep` calls inside the loop??!)
2. Is the assumption that StopIteration will be raised correct? I've seen that in the code of the library: https://github.com/ukyg9e5r6k7gubiekd6/gpsd/blob/master/gps/gps.py#L312

Here is the relevant excerpt

def next(self):
if self.read() == -1:
raise StopIteration
if hasattr(self, "data"):
return self.data
else:
return self.response



This is somewhat puzzling, because as you said - it shouldn't be a complicated matter.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]