mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-24 11:42:52 +01:00
schedule: Add parameters args and kwargs.
This commit is contained in:
parent
bad5364e28
commit
8558640a00
@ -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:')
|
||||
|
||||
|
@ -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)'
|
||||
|
Loading…
Reference in New Issue
Block a user