ž «ÿfÿc@sàdZddlZddlZddlmZyddlZWnek r^ddlZYnXyddlmZ Wn"ek r—ddlmZ YnXdgZ Gdd„dedd ƒƒZ e ƒZ Gd d„dƒZdS( uñA generally useful event scheduler class. Each instance of this class manages its own queue. No multi-threading is implied; you are supposed to hack that yourself, or use a single instance per application. Each instance is parametrized with two functions, one that is supposed to return the current time, one that is supposed to implement a delay. You can implement real-time scheduling by substituting time and sleep from built-in module time, or you can implement simulated time by writing your own functions. This can also be used to integrate scheduling with STDWIN events; the delay function is allowed to modify the queue. Time can be expressed as integers or floating point numbers, as long as it is consistent. Events are specified by tuples (time, priority, action, argument, kwargs). As in UNIX, lower priority numbers mean higher priority; in this way the queue can be maintained as a priority queue. Execution of the event means calling the action function, passing it the argument sequence in "argument" (remember that in Python, multiple function arguments are be packed in a sequence) and keyword parameters in "kwargs". The action function may be an instance method so it has another way to reference private data (besides global variables). iN(u namedtuple(u monotonic(utimeu schedulercBs\|EeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd S(uEventcCs"|j|jf|j|jfkS(N(utimeupriority(usuo((u*/opt/alt/python33/lib64/python3.3/sched.pyu__eq__.su Event.__eq__cCs"|j|jf|j|jfkS(N(utimeupriority(usuo((u*/opt/alt/python33/lib64/python3.3/sched.pyu__ne__/su Event.__ne__cCs"|j|jf|j|jfkS(N(utimeupriority(usuo((u*/opt/alt/python33/lib64/python3.3/sched.pyu__lt__0su Event.__lt__cCs"|j|jf|j|jfkS(N(utimeupriority(usuo((u*/opt/alt/python33/lib64/python3.3/sched.pyu__le__1su Event.__le__cCs"|j|jf|j|jfkS(N(utimeupriority(usuo((u*/opt/alt/python33/lib64/python3.3/sched.pyu__gt__2su Event.__gt__cCs"|j|jf|j|jfkS(N(utimeupriority(usuo((u*/opt/alt/python33/lib64/python3.3/sched.pyu__ge__3su Event.__ge__N( u__name__u __module__u __qualname__u__eq__u__ne__u__lt__u__le__u__gt__u__ge__(u __locals__((u*/opt/alt/python33/lib64/python3.3/sched.pyuEvent-s      uEventu(time, priority, action, argument, kwargscBs†|EeZdZeejdd„Zfedd„Zfedd„Z dd„Z d d „Z dd d „Z ed d„ƒZdS(u schedulercCs.g|_tjƒ|_||_||_dS(uGInitialize a new instance, passing the time and delay functionsN(u_queueu threadinguRLocku_lockutimefuncu delayfunc(uselfutimefuncu delayfunc((u*/opt/alt/python33/lib64/python3.3/sched.pyu__init__9s  uscheduler.__init__c CsX|tkri}n|j4t|||||ƒ}tj|j|ƒ|SWdQXdS(u˜Enter a new event in the queue at an absolute time. Returns an ID for the event which can be used to remove it, if necessary. N(u _sentinelu_lockuEventuheapquheappushu_queue(uselfutimeupriorityuactionuargumentukwargsuevent((u*/opt/alt/python33/lib64/python3.3/sched.pyuenterabsAs    uscheduler.enterabsc Cs=|j.|jƒ|}|j|||||ƒSWdQXdS(u{A variant that specifies the time as a relative time. This is actually the more commonly used interface. N(u_lockutimefuncuenterabs(uselfudelayupriorityuactionuargumentukwargsutime((u*/opt/alt/python33/lib64/python3.3/sched.pyuenterOs uscheduler.enterc Cs4|j%|jj|ƒtj|jƒWdQXdS(uªRemove an event from the queue. This must be presented the ID as returned by enter(). If the event is not in the queue, this raises ValueError. N(u_locku_queueuremoveuheapquheapify(uselfuevent((u*/opt/alt/python33/lib64/python3.3/sched.pyucancelYs uscheduler.cancelcCs|j |j SWdQXdS(u!Check whether the queue is empty.N(u_locku_queue(uself((u*/opt/alt/python33/lib64/python3.3/sched.pyuemptyds uscheduler.emptyc CsÑ|j}|j}|j}|j}tj}x|V|sAPn|d\}}} } } |ƒ} || krxd} nd} ||ƒWdQX| r³|s¢|| S||| ƒq0| | | Ž|dƒq0dS(uExecute events until the queue is empty. If blocking is False executes the scheduled events due to expire soonest (if any) and then return the deadline of the next scheduled call in the scheduler. When there is a positive delay until the first event, the delay function is called and the event is left in the queue; otherwise, the event is removed from the queue and executed (its action function is called, passing it the argument). If the delay function returns prematurely, it is simply restarted. It is legal for both the delay function and the action function to modify the queue or to raise an exception; exceptions are not caught but the scheduler's state remains well-defined so run() may be called again. A questionable hack is added to allow other threads to run: just after an event is executed, a delay of 0 is executed, to avoid monopolizing the CPU when other threads are also runnable. iNTF(u_locku_queueu delayfuncutimefuncuheapquheappopuTrueuFalse(uselfublockingulockuqu delayfuncutimefuncupoputimeupriorityuactionuargumentukwargsunowudelay((u*/opt/alt/python33/lib64/python3.3/sched.pyurunis*         u scheduler.runc CsJ|j;|jdd…}tttj|gt|ƒƒƒSWdQXdS(u•An ordered list of upcoming events. Events are named tuples with fields for: time, priority, action, arguments, kwargs N(u_locku_queueulistumapuheapquheappopulen(uselfuevents((u*/opt/alt/python33/lib64/python3.3/sched.pyuqueue›s uscheduler.queueNT(u__name__u __module__u __qualname__u_timeutimeusleepu__init__u _sentineluenterabsuenterucanceluemptyuTrueurunupropertyuqueue(u __locals__((u*/opt/alt/python33/lib64/python3.3/sched.pyu scheduler7s 2(u__doc__utimeuheapqu collectionsu namedtupleu threadingu ImportErrorudummy_threadingu monotonicu_timeu__all__uEventuobjectu _sentinelu scheduler(((u*/opt/alt/python33/lib64/python3.3/sched.pyus