Nir Soffer has uploaded a new change for review. Change subject: schedule: Remove some fat ......................................................................
schedule: Remove some fat Previously we created a ScheduldedCall wrapper for every call, to allow the caller to cancel the call without accessing call internals. This patch removes this fat by returning the internal object, and renaming the execute method so it is clear that it is not a public interface. For internal objects used by the scheduler, there is no problem to use protected methods. Since we have about 2 scheduled calls per sampling, and we may have 100's of sampling per second, minimizing object creation is a nice property for this library. Change-Id: Ib0c89122c0526071445ba1cb9aa199f9cd6f627a Signed-off-by: Nir Soffer <[email protected]> --- M lib/vdsm/schedule.py 1 file changed, 23 insertions(+), 38 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/51/30751/1 diff --git a/lib/vdsm/schedule.py b/lib/vdsm/schedule.py index 70abd05..2c1081d 100644 --- a/lib/vdsm/schedule.py +++ b/lib/vdsm/schedule.py @@ -111,13 +111,13 @@ yet. """ deadline = time.time() + delay - call = _Call(deadline, callable) + call = ScheduledCall(deadline, callable) with self._cond: if not self._running: raise AssertionError("Scheduler not running") heapq.heappush(self._calls, call) self._cond.notify() - return ScheduledCall(call) + return call @utils.traceback(on=_log.name) def _run(self): @@ -140,7 +140,7 @@ return expired = self._pop_expired_calls() for call in expired: - call.execute() + call._execute() def _time_until_deadline(self): if len(self._calls) > 0: @@ -175,49 +175,34 @@ _log = logging.getLogger("Scheduler") - def __init__(self, call): - self._call = call + def __init__(self, deadline, callable): + self._deadline = deadline + self._callable = callable @property def deadline(self): - return self._call.deadline + return self._deadline def cancel(self): - self._log.debug("Canceling %s", self._call) - self._call.cancel() + self._callable = _INVALID + + def _execute(self): + try: + self._callable() + except Exception: + self._log.exception("Unhandled exception in %s", self) + finally: + self._callable = _INVALID + + def __cmp__(self, other): + return cmp(self._deadline, other._deadline) + + def __str__(self): + return "<ScheduledCall deadline=%.3f callable=%s>" % ( + self._deadline, self._callable) # Sentinel for marking calls as invalid. Callable so we can invalidate a call # in a thread safe manner without locks. def _INVALID(): pass - - -class _Call(object): - """ - Internal object created for each scheduled call. - """ - - _log = logging.getLogger('Scheduler') - - def __init__(self, deadline, callable): - self.deadline = deadline - self.callable = callable - - def execute(self): - try: - self.callable() - except Exception: - self._log.exception("Unhandled exception in %s", self) - finally: - self.callable = _INVALID - - def cancel(self): - self.callable = _INVALID - - def __cmp__(self, other): - return cmp(self.deadline, other.deadline) - - def __str__(self): - return "<_Call deadline=%.3f callable=%s>" % ( - self.deadline, self.callable) -- To view, visit http://gerrit.ovirt.org/30751 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib0c89122c0526071445ba1cb9aa199f9cd6f627a Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Nir Soffer <[email protected]> _______________________________________________ vdsm-patches mailing list [email protected] https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches
