11 def __init__(self, timestamp, callback):
13 self.timestamp = timestamp
14 self.callback = callback
16 self.status = TaskStatus.NEW
18 class HeapScheduler(object):
19 """ This class is thread safe.
20 All calls to C Extensions are made atomic by the GIL in the CPython implementation.
21 heapq.heappush, heapq.heappop, and list access are therefore thread-safe """
24 super(HeapScheduler, self).__init__()
27 self._idgen = itertools.count(1)
29 def schedule(self, task):
31 task.id = self._idgen.next()
32 entry = (task.timestamp, task.id, task)
33 self._valid.add(task.id)
34 heapq.heappush(self._queue, entry)
37 def remove(self, tid):
39 self._valid.remove(tid)
46 timestamp, tid, task = heapq.heappop(self._queue)
47 if tid in self._valid: