Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: sched.py
"""A generally useful event scheduler class.
[0] Fix | Delete
[1] Fix | Delete
Each instance of this class manages its own queue.
[2] Fix | Delete
No multi-threading is implied; you are supposed to hack that
[3] Fix | Delete
yourself, or use a single instance per application.
[4] Fix | Delete
[5] Fix | Delete
Each instance is parametrized with two functions, one that is
[6] Fix | Delete
supposed to return the current time, one that is supposed to
[7] Fix | Delete
implement a delay. You can implement real-time scheduling by
[8] Fix | Delete
substituting time and sleep from built-in module time, or you can
[9] Fix | Delete
implement simulated time by writing your own functions. This can
[10] Fix | Delete
also be used to integrate scheduling with STDWIN events; the delay
[11] Fix | Delete
function is allowed to modify the queue. Time can be expressed as
[12] Fix | Delete
integers or floating point numbers, as long as it is consistent.
[13] Fix | Delete
[14] Fix | Delete
Events are specified by tuples (time, priority, action, argument).
[15] Fix | Delete
As in UNIX, lower priority numbers mean higher priority; in this
[16] Fix | Delete
way the queue can be maintained as a priority queue. Execution of the
[17] Fix | Delete
event means calling the action function, passing it the argument
[18] Fix | Delete
sequence in "argument" (remember that in Python, multiple function
[19] Fix | Delete
arguments are be packed in a sequence).
[20] Fix | Delete
The action function may be an instance method so it
[21] Fix | Delete
has another way to reference private data (besides global variables).
[22] Fix | Delete
"""
[23] Fix | Delete
[24] Fix | Delete
# XXX The timefunc and delayfunc should have been defined as methods
[25] Fix | Delete
# XXX so you can define new kinds of schedulers using subclassing
[26] Fix | Delete
# XXX instead of having to define a module or class just to hold
[27] Fix | Delete
# XXX the global state of your particular time and delay functions.
[28] Fix | Delete
[29] Fix | Delete
import heapq
[30] Fix | Delete
from collections import namedtuple
[31] Fix | Delete
[32] Fix | Delete
__all__ = ["scheduler"]
[33] Fix | Delete
[34] Fix | Delete
Event = namedtuple('Event', 'time, priority, action, argument')
[35] Fix | Delete
[36] Fix | Delete
class scheduler:
[37] Fix | Delete
def __init__(self, timefunc, delayfunc):
[38] Fix | Delete
"""Initialize a new instance, passing the time and delay
[39] Fix | Delete
functions"""
[40] Fix | Delete
self._queue = []
[41] Fix | Delete
self.timefunc = timefunc
[42] Fix | Delete
self.delayfunc = delayfunc
[43] Fix | Delete
[44] Fix | Delete
def enterabs(self, time, priority, action, argument):
[45] Fix | Delete
"""Enter a new event in the queue at an absolute time.
[46] Fix | Delete
[47] Fix | Delete
Returns an ID for the event which can be used to remove it,
[48] Fix | Delete
if necessary.
[49] Fix | Delete
[50] Fix | Delete
"""
[51] Fix | Delete
event = Event(time, priority, action, argument)
[52] Fix | Delete
heapq.heappush(self._queue, event)
[53] Fix | Delete
return event # The ID
[54] Fix | Delete
[55] Fix | Delete
def enter(self, delay, priority, action, argument):
[56] Fix | Delete
"""A variant that specifies the time as a relative time.
[57] Fix | Delete
[58] Fix | Delete
This is actually the more commonly used interface.
[59] Fix | Delete
[60] Fix | Delete
"""
[61] Fix | Delete
time = self.timefunc() + delay
[62] Fix | Delete
return self.enterabs(time, priority, action, argument)
[63] Fix | Delete
[64] Fix | Delete
def cancel(self, event):
[65] Fix | Delete
"""Remove an event from the queue.
[66] Fix | Delete
[67] Fix | Delete
This must be presented the ID as returned by enter().
[68] Fix | Delete
If the event is not in the queue, this raises ValueError.
[69] Fix | Delete
[70] Fix | Delete
"""
[71] Fix | Delete
self._queue.remove(event)
[72] Fix | Delete
heapq.heapify(self._queue)
[73] Fix | Delete
[74] Fix | Delete
def empty(self):
[75] Fix | Delete
"""Check whether the queue is empty."""
[76] Fix | Delete
return not self._queue
[77] Fix | Delete
[78] Fix | Delete
def run(self):
[79] Fix | Delete
"""Execute events until the queue is empty.
[80] Fix | Delete
[81] Fix | Delete
When there is a positive delay until the first event, the
[82] Fix | Delete
delay function is called and the event is left in the queue;
[83] Fix | Delete
otherwise, the event is removed from the queue and executed
[84] Fix | Delete
(its action function is called, passing it the argument). If
[85] Fix | Delete
the delay function returns prematurely, it is simply
[86] Fix | Delete
restarted.
[87] Fix | Delete
[88] Fix | Delete
It is legal for both the delay function and the action
[89] Fix | Delete
function to modify the queue or to raise an exception;
[90] Fix | Delete
exceptions are not caught but the scheduler's state remains
[91] Fix | Delete
well-defined so run() may be called again.
[92] Fix | Delete
[93] Fix | Delete
A questionable hack is added to allow other threads to run:
[94] Fix | Delete
just after an event is executed, a delay of 0 is executed, to
[95] Fix | Delete
avoid monopolizing the CPU when other threads are also
[96] Fix | Delete
runnable.
[97] Fix | Delete
[98] Fix | Delete
"""
[99] Fix | Delete
# localize variable access to minimize overhead
[100] Fix | Delete
# and to improve thread safety
[101] Fix | Delete
q = self._queue
[102] Fix | Delete
delayfunc = self.delayfunc
[103] Fix | Delete
timefunc = self.timefunc
[104] Fix | Delete
pop = heapq.heappop
[105] Fix | Delete
while q:
[106] Fix | Delete
time, priority, action, argument = checked_event = q[0]
[107] Fix | Delete
now = timefunc()
[108] Fix | Delete
if now < time:
[109] Fix | Delete
delayfunc(time - now)
[110] Fix | Delete
else:
[111] Fix | Delete
event = pop(q)
[112] Fix | Delete
# Verify that the event was not removed or altered
[113] Fix | Delete
# by another thread after we last looked at q[0].
[114] Fix | Delete
if event is checked_event:
[115] Fix | Delete
action(*argument)
[116] Fix | Delete
delayfunc(0) # Let other threads run
[117] Fix | Delete
else:
[118] Fix | Delete
heapq.heappush(q, event)
[119] Fix | Delete
[120] Fix | Delete
@property
[121] Fix | Delete
def queue(self):
[122] Fix | Delete
"""An ordered list of upcoming events.
[123] Fix | Delete
[124] Fix | Delete
Events are named tuples with fields for:
[125] Fix | Delete
time, priority, action, arguments
[126] Fix | Delete
[127] Fix | Delete
"""
[128] Fix | Delete
# Use heapq to sort the queue rather than using 'sorted(self._queue)'.
[129] Fix | Delete
# With heapq, two events scheduled at the same time will show in
[130] Fix | Delete
# the actual order they would be retrieved.
[131] Fix | Delete
events = self._queue[:]
[132] Fix | Delete
return map(heapq.heappop, [events]*len(events))
[133] Fix | Delete
[134] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function