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