sched.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """A generally useful event scheduler class.
  2. Each instance of this class manages its own queue.
  3. No multi-threading is implied; you are supposed to hack that
  4. yourself, or use a single instance per application.
  5. Each instance is parametrized with two functions, one that is
  6. supposed to return the current time, one that is supposed to
  7. implement a delay. You can implement real-time scheduling by
  8. substituting time and sleep from built-in module time, or you can
  9. implement simulated time by writing your own functions. This can
  10. also be used to integrate scheduling with STDWIN events; the delay
  11. function is allowed to modify the queue. Time can be expressed as
  12. integers or floating-point numbers, as long as it is consistent.
  13. Events are specified by tuples (time, priority, action, argument, kwargs).
  14. As in UNIX, lower priority numbers mean higher priority; in this
  15. way the queue can be maintained as a priority queue. Execution of the
  16. event means calling the action function, passing it the argument
  17. sequence in "argument" (remember that in Python, multiple function
  18. arguments are be packed in a sequence) and keyword parameters in "kwargs".
  19. The action function may be an instance method so it
  20. has another way to reference private data (besides global variables).
  21. """
  22. import time
  23. import heapq
  24. from collections import namedtuple
  25. from itertools import count
  26. import threading
  27. from time import monotonic as _time
  28. __all__ = ["scheduler"]
  29. Event = namedtuple('Event', 'time, priority, sequence, action, argument, kwargs')
  30. Event.time.__doc__ = ('''Numeric type compatible with the return value of the
  31. timefunc function passed to the constructor.''')
  32. Event.priority.__doc__ = ('''Events scheduled for the same time will be executed
  33. in the order of their priority.''')
  34. Event.sequence.__doc__ = ('''A continually increasing sequence number that
  35. separates events if time and priority are equal.''')
  36. Event.action.__doc__ = ('''Executing the event means executing
  37. action(*argument, **kwargs)''')
  38. Event.argument.__doc__ = ('''argument is a sequence holding the positional
  39. arguments for the action.''')
  40. Event.kwargs.__doc__ = ('''kwargs is a dictionary holding the keyword
  41. arguments for the action.''')
  42. _sentinel = object()
  43. class scheduler:
  44. def __init__(self, timefunc=_time, delayfunc=time.sleep):
  45. """Initialize a new instance, passing the time and delay
  46. functions"""
  47. self._queue = []
  48. self._lock = threading.RLock()
  49. self.timefunc = timefunc
  50. self.delayfunc = delayfunc
  51. self._sequence_generator = count()
  52. def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel):
  53. """Enter a new event in the queue at an absolute time.
  54. Returns an ID for the event which can be used to remove it,
  55. if necessary.
  56. """
  57. if kwargs is _sentinel:
  58. kwargs = {}
  59. with self._lock:
  60. event = Event(time, priority, next(self._sequence_generator),
  61. action, argument, kwargs)
  62. heapq.heappush(self._queue, event)
  63. return event # The ID
  64. def enter(self, delay, priority, action, argument=(), kwargs=_sentinel):
  65. """A variant that specifies the time as a relative time.
  66. This is actually the more commonly used interface.
  67. """
  68. time = self.timefunc() + delay
  69. return self.enterabs(time, priority, action, argument, kwargs)
  70. def cancel(self, event):
  71. """Remove an event from the queue.
  72. This must be presented the ID as returned by enter().
  73. If the event is not in the queue, this raises ValueError.
  74. """
  75. with self._lock:
  76. self._queue.remove(event)
  77. heapq.heapify(self._queue)
  78. def empty(self):
  79. """Check whether the queue is empty."""
  80. with self._lock:
  81. return not self._queue
  82. def run(self, blocking=True):
  83. """Execute events until the queue is empty.
  84. If blocking is False executes the scheduled events due to
  85. expire soonest (if any) and then return the deadline of the
  86. next scheduled call in the scheduler.
  87. When there is a positive delay until the first event, the
  88. delay function is called and the event is left in the queue;
  89. otherwise, the event is removed from the queue and executed
  90. (its action function is called, passing it the argument). If
  91. the delay function returns prematurely, it is simply
  92. restarted.
  93. It is legal for both the delay function and the action
  94. function to modify the queue or to raise an exception;
  95. exceptions are not caught but the scheduler's state remains
  96. well-defined so run() may be called again.
  97. A questionable hack is added to allow other threads to run:
  98. just after an event is executed, a delay of 0 is executed, to
  99. avoid monopolizing the CPU when other threads are also
  100. runnable.
  101. """
  102. # localize variable access to minimize overhead
  103. # and to improve thread safety
  104. lock = self._lock
  105. q = self._queue
  106. delayfunc = self.delayfunc
  107. timefunc = self.timefunc
  108. pop = heapq.heappop
  109. while True:
  110. with lock:
  111. if not q:
  112. break
  113. (time, priority, sequence, action,
  114. argument, kwargs) = q[0]
  115. now = timefunc()
  116. if time > now:
  117. delay = True
  118. else:
  119. delay = False
  120. pop(q)
  121. if delay:
  122. if not blocking:
  123. return time - now
  124. delayfunc(time - now)
  125. else:
  126. action(*argument, **kwargs)
  127. delayfunc(0) # Let other threads run
  128. @property
  129. def queue(self):
  130. """An ordered list of upcoming events.
  131. Events are named tuples with fields for:
  132. time, priority, action, arguments, kwargs
  133. """
  134. # Use heapq to sort the queue rather than using 'sorted(self._queue)'.
  135. # With heapq, two events scheduled at the same time will show in
  136. # the actual order they would be retrieved.
  137. with self._lock:
  138. events = self._queue[:]
  139. return list(map(heapq.heappop, [events]*len(events)))