Advertisement
Guest User

Untitled

a guest
May 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. import time
  2. from collections import deque
  3.  
  4.  
  5. class Server:
  6.     def __init__(self, dt):
  7.         self.dt = dt
  8.         self.queue = deque()
  9.         self.last_ids = set()
  10.         self.all_ids = {}
  11.  
  12.     def register_event(self, id):
  13.         self.queue.append((id, time.time()))
  14.         self.all_ids[id] += 1
  15.         if self.all_ids[id] > 1000:
  16.             self.last_ids.add(id)
  17.  
  18.     def check(self, ntime):
  19.         while len(self.queue):
  20.             q = self.queue.popleft()
  21.             if ntime - q[1] <= self.dt:
  22.                 self.queue.appendleft(q)
  23.                 break
  24.             self.all_ids[q[0]] -= 1
  25.             if self.all_ids[id] < 1000 and q[0] in self.last_ids:
  26.                 self.last_ids.remove(q[0])
  27.  
  28.     def get_ids(self):
  29.         self.check(time.time())
  30.         return self.last_ids
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement