First commit
This commit is contained in:
27
queue.py
Normal file
27
queue.py
Normal file
@@ -0,0 +1,27 @@
|
||||
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
|
||||
Reference in New Issue
Block a user