OBP60v/nmea2000/queue.py

28 lines
682 B
Python

from heapdict import heapdict
import time
import threading
class FrameQueue():
def __init__(self):
self.heap = heapdict()
self.age_interval = 5 # seconds
def push(self, frame, priority):
timestamp = time.time() # microsecond resolution
self.heap[timestamp] = (priority, timestamp, frame)
def pop(self):
p, f = self.heap.popitem()
self.age()
return f[2]
def age(self):
current_time = time.time()
for key in list(self.heap.keys()):
if current_time - key > self.age_interval:
self.heap[key][0] -= 1
def is_empty(self):
return len(self.heap) == 0