Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
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, kwargs).
[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) and keyword parameters in "kwargs".
[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
import time
[25] Fix | Delete
import heapq
[26] Fix | Delete
from collections import namedtuple
[27] Fix | Delete
import threading
[28] Fix | Delete
from time import monotonic as _time
[29] Fix | Delete
[30] Fix | Delete
__all__ = ["scheduler"]
[31] Fix | Delete
[32] Fix | Delete
class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
[33] Fix | Delete
__slots__ = []
[34] Fix | Delete
def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority)
[35] Fix | Delete
def __lt__(s, o): return (s.time, s.priority) < (o.time, o.priority)
[36] Fix | Delete
def __le__(s, o): return (s.time, s.priority) <= (o.time, o.priority)
[37] Fix | Delete
def __gt__(s, o): return (s.time, s.priority) > (o.time, o.priority)
[38] Fix | Delete
def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)
[39] Fix | Delete
[40] Fix | Delete
Event.time.__doc__ = ('''Numeric type compatible with the return value of the
[41] Fix | Delete
timefunc function passed to the constructor.''')
[42] Fix | Delete
Event.priority.__doc__ = ('''Events scheduled for the same time will be executed
[43] Fix | Delete
in the order of their priority.''')
[44] Fix | Delete
Event.action.__doc__ = ('''Executing the event means executing
[45] Fix | Delete
action(*argument, **kwargs)''')
[46] Fix | Delete
Event.argument.__doc__ = ('''argument is a sequence holding the positional
[47] Fix | Delete
arguments for the action.''')
[48] Fix | Delete
Event.kwargs.__doc__ = ('''kwargs is a dictionary holding the keyword
[49] Fix | Delete
arguments for the action.''')
[50] Fix | Delete
[51] Fix | Delete
_sentinel = object()
[52] Fix | Delete
[53] Fix | Delete
class scheduler:
[54] Fix | Delete
[55] Fix | Delete
def __init__(self, timefunc=_time, delayfunc=time.sleep):
[56] Fix | Delete
"""Initialize a new instance, passing the time and delay
[57] Fix | Delete
functions"""
[58] Fix | Delete
self._queue = []
[59] Fix | Delete
self._lock = threading.RLock()
[60] Fix | Delete
self.timefunc = timefunc
[61] Fix | Delete
self.delayfunc = delayfunc
[62] Fix | Delete
[63] Fix | Delete
def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel):
[64] Fix | Delete
"""Enter a new event in the queue at an absolute time.
[65] Fix | Delete
[66] Fix | Delete
Returns an ID for the event which can be used to remove it,
[67] Fix | Delete
if necessary.
[68] Fix | Delete
[69] Fix | Delete
"""
[70] Fix | Delete
if kwargs is _sentinel:
[71] Fix | Delete
kwargs = {}
[72] Fix | Delete
event = Event(time, priority, action, argument, kwargs)
[73] Fix | Delete
with self._lock:
[74] Fix | Delete
heapq.heappush(self._queue, event)
[75] Fix | Delete
return event # The ID
[76] Fix | Delete
[77] Fix | Delete
def enter(self, delay, priority, action, argument=(), kwargs=_sentinel):
[78] Fix | Delete
"""A variant that specifies the time as a relative time.
[79] Fix | Delete
[80] Fix | Delete
This is actually the more commonly used interface.
[81] Fix | Delete
[82] Fix | Delete
"""
[83] Fix | Delete
time = self.timefunc() + delay
[84] Fix | Delete
return self.enterabs(time, priority, action, argument, kwargs)
[85] Fix | Delete
[86] Fix | Delete
def cancel(self, event):
[87] Fix | Delete
"""Remove an event from the queue.
[88] Fix | Delete
[89] Fix | Delete
This must be presented the ID as returned by enter().
[90] Fix | Delete
If the event is not in the queue, this raises ValueError.
[91] Fix | Delete
[92] Fix | Delete
"""
[93] Fix | Delete
with self._lock:
[94] Fix | Delete
self._queue.remove(event)
[95] Fix | Delete
heapq.heapify(self._queue)
[96] Fix | Delete
[97] Fix | Delete
def empty(self):
[98] Fix | Delete
"""Check whether the queue is empty."""
[99] Fix | Delete
with self._lock:
[100] Fix | Delete
return not self._queue
[101] Fix | Delete
[102] Fix | Delete
def run(self, blocking=True):
[103] Fix | Delete
"""Execute events until the queue is empty.
[104] Fix | Delete
If blocking is False executes the scheduled events due to
[105] Fix | Delete
expire soonest (if any) and then return the deadline of the
[106] Fix | Delete
next scheduled call in the scheduler.
[107] Fix | Delete
[108] Fix | Delete
When there is a positive delay until the first event, the
[109] Fix | Delete
delay function is called and the event is left in the queue;
[110] Fix | Delete
otherwise, the event is removed from the queue and executed
[111] Fix | Delete
(its action function is called, passing it the argument). If
[112] Fix | Delete
the delay function returns prematurely, it is simply
[113] Fix | Delete
restarted.
[114] Fix | Delete
[115] Fix | Delete
It is legal for both the delay function and the action
[116] Fix | Delete
function to modify the queue or to raise an exception;
[117] Fix | Delete
exceptions are not caught but the scheduler's state remains
[118] Fix | Delete
well-defined so run() may be called again.
[119] Fix | Delete
[120] Fix | Delete
A questionable hack is added to allow other threads to run:
[121] Fix | Delete
just after an event is executed, a delay of 0 is executed, to
[122] Fix | Delete
avoid monopolizing the CPU when other threads are also
[123] Fix | Delete
runnable.
[124] Fix | Delete
[125] Fix | Delete
"""
[126] Fix | Delete
# localize variable access to minimize overhead
[127] Fix | Delete
# and to improve thread safety
[128] Fix | Delete
lock = self._lock
[129] Fix | Delete
q = self._queue
[130] Fix | Delete
delayfunc = self.delayfunc
[131] Fix | Delete
timefunc = self.timefunc
[132] Fix | Delete
pop = heapq.heappop
[133] Fix | Delete
while True:
[134] Fix | Delete
with lock:
[135] Fix | Delete
if not q:
[136] Fix | Delete
break
[137] Fix | Delete
time, priority, action, argument, kwargs = q[0]
[138] Fix | Delete
now = timefunc()
[139] Fix | Delete
if time > now:
[140] Fix | Delete
delay = True
[141] Fix | Delete
else:
[142] Fix | Delete
delay = False
[143] Fix | Delete
pop(q)
[144] Fix | Delete
if delay:
[145] Fix | Delete
if not blocking:
[146] Fix | Delete
return time - now
[147] Fix | Delete
delayfunc(time - now)
[148] Fix | Delete
else:
[149] Fix | Delete
action(*argument, **kwargs)
[150] Fix | Delete
delayfunc(0) # Let other threads run
[151] Fix | Delete
[152] Fix | Delete
@property
[153] Fix | Delete
def queue(self):
[154] Fix | Delete
"""An ordered list of upcoming events.
[155] Fix | Delete
[156] Fix | Delete
Events are named tuples with fields for:
[157] Fix | Delete
time, priority, action, arguments, kwargs
[158] Fix | Delete
[159] Fix | Delete
"""
[160] Fix | Delete
# Use heapq to sort the queue rather than using 'sorted(self._queue)'.
[161] Fix | Delete
# With heapq, two events scheduled at the same time will show in
[162] Fix | Delete
# the actual order they would be retrieved.
[163] Fix | Delete
with self._lock:
[164] Fix | Delete
events = self._queue[:]
[165] Fix | Delete
return list(map(heapq.heappop, [events]*len(events)))
[166] Fix | Delete
[167] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function