schedule: Add parameters args and kwargs.

This commit is contained in:
Valentin Lorentz 2012-04-04 15:55:08 +02:00
parent bad5364e28
commit 8558640a00
2 changed files with 6 additions and 6 deletions

View File

@ -72,7 +72,7 @@ class Schedule(drivers.IrcDriver):
def name(self):
return 'Schedule'
def addEvent(self, f, t, name=None):
def addEvent(self, f, t, name=None, args=[], kwargs={}):
"""Schedules an event f to run at time t.
name must be hashable and not an int.
@ -83,13 +83,13 @@ class Schedule(drivers.IrcDriver):
assert name not in self.events, \
'An event with the same name has already been scheduled.'
self.events[name] = f
heapq.heappush(self.schedule, mytuple((t, name)))
heapq.heappush(self.schedule, mytuple((t, name, args, kwargs)))
return name
def removeEvent(self, name):
"""Removes the event with the given name from the schedule."""
f = self.events.pop(name)
self.schedule = [(t, n) for (t, n) in self.schedule if n != name]
self.schedule = [x for x in self.schedule if n != name]
# We must heapify here because the heap property may not be preserved
# by the above list comprehension. We could, conceivably, just mark
# the elements of the heap as removed and ignore them when we heappop,
@ -123,11 +123,11 @@ class Schedule(drivers.IrcDriver):
'why do we continue to live?')
time.sleep(1) # We're the only driver; let's pause to think.
while self.schedule and self.schedule[0][0] < time.time():
(t, name) = heapq.heappop(self.schedule)
(t, name, args, kwargs) = heapq.heappop(self.schedule)
f = self.events[name]
del self.events[name]
try:
f()
f(*args, **kwargs)
except Exception, e:
log.exception('Uncaught exception in scheduled function:')

View File

@ -1,3 +1,3 @@
"""stick the various versioning attributes in here, so we only have to change
them once."""
version = '0.83.4.1+limnoria (2012-04-04T13:13:39+0000)'
version = '0.83.4.1+limnoria (2012-04-04T13:55:08+0000)'