X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=doc%2Fsphinx%2F_build%2Fhtml%2F_modules%2Fnepi%2Fexecution%2Fscheduler.html;fp=doc%2Fsphinx%2F_build%2Fhtml%2F_modules%2Fnepi%2Fexecution%2Fscheduler.html;h=c10d305c864c78dbc1722dd028bba27630418e9c;hb=9b6a92c1bb6c85d1b6d194624e49ea01ce3893e7;hp=0000000000000000000000000000000000000000;hpb=ed00cc0dfbe89c748c07b7da96dfff338cdb301a;p=nepi.git diff --git a/doc/sphinx/_build/html/_modules/nepi/execution/scheduler.html b/doc/sphinx/_build/html/_modules/nepi/execution/scheduler.html new file mode 100644 index 00000000..c10d305c --- /dev/null +++ b/doc/sphinx/_build/html/_modules/nepi/execution/scheduler.html @@ -0,0 +1,201 @@ + + + + +
+ + +
+#
+# NEPI, a framework to manage network experiments
+# Copyright (C) 2013 INRIA
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation;
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: Alina Quereilhac <alina.quereilhac@inria.fr>
+
+import itertools
+import heapq
+
+
+[docs]class Task(object):
+ """ A Task represents an operation to be executed by the
+ ExperimentController scheduler
+ """
+
+ def __init__(self, timestamp, callback):
+ """
+ :param timestamp: Future execution date of the operation
+ :type timestamp: str
+
+ :param callback: A function to invoke in order to execute the operation
+ :type callback: function
+
+ """
+ self.id = None
+ self.timestamp = timestamp
+ self.callback = callback
+ self.result = None
+ self.status = TaskStatus.NEW
+
+[docs]class HeapScheduler(object):
+ """ Create a Heap Scheduler
+
+ .. note::
+
+ This class is thread safe.
+ All calls to C Extensions are made atomic by the GIL in the CPython implementation.
+ heapq.heappush, heapq.heappop, and list access are therefore thread-safe.
+
+ """
+
+ def __init__(self):
+ super(HeapScheduler, self).__init__()
+ self._queue = list()
+ self._valid = set()
+ self._idgen = itertools.count(1)
+
+ @property
+
+[docs] def schedule(self, task):
+ """ Add a task to the queue ordered by task.timestamp and arrival order
+
+ :param task: task to schedule
+ :type task: task
+ """
+ if task.id == None:
+ task.id = self._idgen.next()
+
+ entry = (task.timestamp, task.id, task)
+ self._valid.add(task.id)
+ heapq.heappush(self._queue, entry)
+ return task
+
+[docs] def remove(self, tid):
+ """ Remove a task form the queue
+
+ :param tid: Id of the task to be removed
+ :type tid: int
+
+ """
+ try:
+ self._valid.remove(tid)
+ except:
+ pass
+
+[docs] def next(self):
+ """ Get the next task in the queue by timestamp and arrival order
+ """
+ while self._queue:
+ try:
+ timestamp, tid, task = heapq.heappop(self._queue)
+ if tid in self._valid:
+ self.remove(tid)
+ return task
+ except IndexError:
+ # heap empty
+ pass
+ return None
+