Can I somehow avoid using time.sleep () in this script?

I have the following python script:

#! /usr/bin/python

import os
from gps import *
from time import *
import time
import threading
import sys

gpsd = None #seting the global variable

class GpsPoller(threading.Thread):
   def __init__(self):
      threading.Thread.__init__(self)
      global gpsd #bring it in scope
      gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
      self.current_value = None
      self.running = True #setting the thread running to true

   def run(self):
      global gpsd
      while gpsp.running:
         gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer

if __name__ == '__main__':
   gpsp = GpsPoller() # create the thread
   try:
      gpsp.start() # start it up
      while True:

         print gpsd.fix.speed

         time.sleep(1) ## <<<< THIS LINE HERE

   except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
      print "\nKilling Thread..."
      gpsp.running = False
      gpsp.join() # wait for the thread to finish what it doing
   print "Done.\nExiting."

I am not very good at python, unfortunately. The script should be multi-threaded somehow (but that probably doesn't matter as part of this question).

What puzzles me is the line gpsd.next(). If I understood correctly, he should have told the script that new gps data was received and ready to read.

However, I read the data using an infinite loop while Truewith a pause of 1 second s time.sleep(1).

However, this means that it sometimes repeats the same data twice (the sensor did not update the data in the last second). I suppose it also skips some sensor data somehow too.

- script, , , ? ( 1 ), , , 1 , .

+4
2

, " ". , , . . gpsd.next() - .

Queue . "" ( ) . queue.get() .

script , , gpsd.next().

:

from gps import *

class GpsPoller(object):
   def __init__(self, action):
      self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
      self.action=action

   def run(self):
      while True:
         self.gpsd.next()
         self.action(self.gpsd)

def myaction(gpsd):
    print gpsd.fix.speed

if __name__ == '__main__':
   gpsp = GpsPoller(myaction)
   gpsp.run() # runs until killed by Ctrl-C

, action .

poller script, (, ), . , GpsPoller:

from threading import Thread
from Queue import Queue

class GpsThread(object):
    def __init__(self, valuefunc, queue):
        self.valuefunc = valuefunc
        self.queue = queue
        self.poller = GpsPoller(self.on_value)

    def start(self):
        self.t = Thread(target=self.poller.run)
        self.t.daemon = True  # kill thread when main thread exits
        self.t.start()

    def on_value(self, gpsd):
        # note that we extract the value right here.
        # Otherwise it could change while the event is in the queue.
        self.queue.put(('gps', self.valuefunc(gpsd)))


def main():
    q = Queue()
    gt = GpsThread(
            valuefunc=lambda gpsd: gpsd.fix.speed,
            queue = q
            )
    print 'press Ctrl-C to stop.'
    gt.start()
    while True:
        # blocks while q is empty.
        source, data = q.get()
        if source == 'gps':
            print data

"", GpsPoller, : " func ". Mainloop , , .

.

+2

:

  • GpsPoller ,
  • GpsPoller .

№1:

global is_speed_changed = False

def run(self):
      global gpsd, is_speed_changed
      while gpsp.running:
         prev_speed = gpsd.fix.speed
         gpsd.next()
         if prev_speed != gpsd.fix.speed
            is_speed_changed = True  # raising flag

while True:
        if is_speed_changed:
           print gpsd.fix.speed
           is_speed_changed = False

№ 2 ( , ):

gpsd_queue = Queue.Queue()

def run(self):
      global gpsd
      while gpsp.running:
         prev_speed = gpsd.fix.speed
         gpsd.next()
         curr_speed = gpsd.fix.speed
         if prev_speed != curr_speed:
            gpsd_queue.put(curr_speed) # putting new speed to queue

while True:
    # get will block if queue is empty 
    print gpsd_queue.get()
+1

Source: https://habr.com/ru/post/1650440/


All Articles