Revision f5acf5d9 lib/daemon.py

b/lib/daemon.py
75 75

  
76 76
  """
77 77
  def __init__(self, timefunc):
78
    sched.scheduler.__init__(self, timefunc, AsyncoreDelayFunction)
78
    """Initializes this class.
79

  
80
    """
81
    sched.scheduler.__init__(self, timefunc, self._LimitedDelay)
82
    self._max_delay = None
83

  
84
  def run(self, max_delay=None): # pylint: disable=W0221
85
    """Run any pending events.
86

  
87
    @type max_delay: None or number
88
    @param max_delay: Maximum delay (useful if caller has timeouts running)
89

  
90
    """
91
    assert self._max_delay is None
92

  
93
    # The delay function used by the scheduler can't be different on each run,
94
    # hence an instance variable must be used.
95
    if max_delay is None:
96
      self._max_delay = None
97
    else:
98
      self._max_delay = utils.RunningTimeout(max_delay, False)
99

  
100
    try:
101
      return sched.scheduler.run(self)
102
    finally:
103
      self._max_delay = None
104

  
105
  def _LimitedDelay(self, duration):
106
    """Custom delay function for C{sched.scheduler}.
107

  
108
    """
109
    if self._max_delay is None:
110
      timeout = duration
111
    else:
112
      timeout = min(duration, self._max_delay.Remaining())
113

  
114
    return AsyncoreDelayFunction(timeout)
79 115

  
80 116

  
81 117
class GanetiBaseAsyncoreDispatcher(asyncore.dispatcher):

Also available in: Unified diff