5 def __init__(self, timestamp, callback):
7 self.timestamp = timestamp
8 self.callback = callback
10 class HeapScheduler(object):
11 """ This class is thread safe.
12 All calls to C Extensions are made atomic by the GIL in the CPython implementation.
13 heapq.heappush, heapq.heappop, and list access are therefore thread-safe """
16 super(HeapScheduler, self).__init__()
19 self._idgen = itertools.count(1)
21 def schedule(self, task):
23 task.id = self._idgen.next()
24 entry = (task.timestamp, task.id, task)
25 self._valid.add(task.id)
26 heapq.heappush(self._queue, entry)
29 def remove(self, tid):
31 self._valid.remove(tid)
38 timestamp, tid, task = heapq.heappop(self._queue)
39 if tid in self._valid: