summaryrefslogtreecommitdiffstats
path: root/Lib/sched.py
diff options
context:
space:
mode:
authorGiampaolo Rodola' <g.rodola@gmail.com>2011-11-22 12:33:34 (GMT)
committerGiampaolo Rodola' <g.rodola@gmail.com>2011-11-22 12:33:34 (GMT)
commitbe55d99b3d0d289b2948fdcb6c8aa975687476d5 (patch)
tree64798196c6bb7ffb76a8630af119f07c239dd331 /Lib/sched.py
parentca4f20782e57ca4c754f477b19d36ac23e456b39 (diff)
downloadcpython-be55d99b3d0d289b2948fdcb6c8aa975687476d5.zip
cpython-be55d99b3d0d289b2948fdcb6c8aa975687476d5.tar.gz
cpython-be55d99b3d0d289b2948fdcb6c8aa975687476d5.tar.bz2
Fix 13245:
sched.scheduler class constructor's timefunc and delayfunct parameters are now optional. scheduler.enter and scheduler.enterabs methods gained a new kwargs parameter. Patch contributed by Matt Mulsow.
Diffstat (limited to 'Lib/sched.py')
-rw-r--r--Lib/sched.py18
1 files changed, 10 insertions, 8 deletions
diff --git a/Lib/sched.py b/Lib/sched.py
index a119892..6c01e69 100644
--- a/Lib/sched.py
+++ b/Lib/sched.py
@@ -28,12 +28,13 @@ has another way to reference private data (besides global variables).
# XXX instead of having to define a module or class just to hold
# XXX the global state of your particular time and delay functions.
+import time
import heapq
from collections import namedtuple
__all__ = ["scheduler"]
-class Event(namedtuple('Event', 'time, priority, action, argument')):
+class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority)
def __ne__(s, o): return (s.time, s.priority) != (o.time, o.priority)
def __lt__(s, o): return (s.time, s.priority) < (o.time, o.priority)
@@ -42,32 +43,33 @@ class Event(namedtuple('Event', 'time, priority, action, argument')):
def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)
class scheduler:
- def __init__(self, timefunc, delayfunc):
+
+ def __init__(self, timefunc=time.time, delayfunc=time.sleep):
"""Initialize a new instance, passing the time and delay
functions"""
self._queue = []
self.timefunc = timefunc
self.delayfunc = delayfunc
- def enterabs(self, time, priority, action, argument):
+ def enterabs(self, time, priority, action, argument=[], kwargs={}):
"""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.
"""
- event = Event(time, priority, action, argument)
+ event = Event(time, priority, action, argument, kwargs)
heapq.heappush(self._queue, event)
return event # The ID
- def enter(self, delay, priority, action, argument):
+ def enter(self, delay, priority, action, argument=[], kwargs={}):
"""A variant that specifies the time as a relative time.
This is actually the more commonly used interface.
"""
time = self.timefunc() + delay
- return self.enterabs(time, priority, action, argument)
+ return self.enterabs(time, priority, action, argument, kwargs)
def cancel(self, event):
"""Remove an event from the queue.
@@ -111,7 +113,7 @@ class scheduler:
timefunc = self.timefunc
pop = heapq.heappop
while q:
- time, priority, action, argument = checked_event = q[0]
+ time, priority, action, argument, kwargs = checked_event = q[0]
now = timefunc()
if now < time:
delayfunc(time - now)
@@ -120,7 +122,7 @@ class scheduler:
# Verify that the event was not removed or altered
# by another thread after we last looked at q[0].
if event is checked_event:
- action(*argument)
+ action(*argument, **kwargs)
delayfunc(0) # Let other threads run
else:
heapq.heappush(q, event)