4 class HeapScheduler(object):
5 """ This class is thread safe.
6 All calls to C Extensions are made atomic by the GIL in the CPython implementation.
7 heapq.heappush, heapq.heappop, and list access are therefore thread-safe """
10 super(HeapScheduler, self).__init__()
13 self._idgen = itertools.count(1)
15 def schedule(self, task):
17 task.id = self._idgen.next()
18 entry = (task.timestamp, task.id, task)
19 self._valid.add(task.id)
20 heapq.heappush(self._queue, entry)
23 def remove(self, tid):
25 self._valid.remove(tid)
32 timestamp, tid, task = heapq.heappop(self._queue)
33 if tid in self._valid: