mirror of
https://github.com/progval/Limnoria.git
synced 2025-04-26 04:51:06 -05:00
Added rescheduleEvent.
This commit is contained in:
parent
55f5f341ce
commit
ad1b36a643
@ -85,8 +85,6 @@ class Schedule(drivers.IrcDriver):
|
|||||||
if name is None:
|
if name is None:
|
||||||
name = self.counter
|
name = self.counter
|
||||||
self.counter += 1
|
self.counter += 1
|
||||||
elif isinstance(name, int):
|
|
||||||
raise ValueError, 'int names are reserved for the scheduler.'
|
|
||||||
assert name not in self.events
|
assert name not in self.events
|
||||||
self.events[name] = f
|
self.events[name] = f
|
||||||
heapq.heappush(self.schedule, mytuple((t, name)))
|
heapq.heappush(self.schedule, mytuple((t, name)))
|
||||||
@ -94,7 +92,7 @@ class Schedule(drivers.IrcDriver):
|
|||||||
|
|
||||||
def removeEvent(self, name):
|
def removeEvent(self, name):
|
||||||
"""Removes the event with the given name from the schedule."""
|
"""Removes the event with the given name from the schedule."""
|
||||||
del self.events[name]
|
f = self.events.pop(name)
|
||||||
self.schedule = [(t, n) for (t, n) in self.schedule if n != name]
|
self.schedule = [(t, n) for (t, n) in self.schedule if n != name]
|
||||||
# We must heapify here because the heap property may not be preserved
|
# We must heapify here because the heap property may not be preserved
|
||||||
# by the above list comprehension. We could, conceivably, just mark
|
# by the above list comprehension. We could, conceivably, just mark
|
||||||
@ -102,6 +100,11 @@ class Schedule(drivers.IrcDriver):
|
|||||||
# but that would only save a constant factor (we're already linear for
|
# but that would only save a constant factor (we're already linear for
|
||||||
# the listcomp) so I'm not worried about it right now.
|
# the listcomp) so I'm not worried about it right now.
|
||||||
heapq.heapify(self.schedule)
|
heapq.heapify(self.schedule)
|
||||||
|
return f
|
||||||
|
|
||||||
|
def rescheduleEvent(self, name, t):
|
||||||
|
f = self.removeEvent(name)
|
||||||
|
self.addEvent(f, t, name=name)
|
||||||
|
|
||||||
def addPeriodicEvent(self, f, t, name=None):
|
def addPeriodicEvent(self, f, t, name=None):
|
||||||
"""Adds a periodic event that is called every t seconds."""
|
"""Adds a periodic event that is called every t seconds."""
|
||||||
@ -134,6 +137,7 @@ except NameError:
|
|||||||
|
|
||||||
addEvent = schedule.addEvent
|
addEvent = schedule.addEvent
|
||||||
removeEvent = schedule.removeEvent
|
removeEvent = schedule.removeEvent
|
||||||
|
rescheduleEvent = schedule.rescheduleEvent
|
||||||
addPeriodicEvent = schedule.addPeriodicEvent
|
addPeriodicEvent = schedule.addPeriodicEvent
|
||||||
removePeriodicEvent = removeEvent
|
removePeriodicEvent = removeEvent
|
||||||
run = schedule.run
|
run = schedule.run
|
||||||
|
@ -61,6 +61,19 @@ class TestSchedule(SupyTestCase):
|
|||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
self.assertEqual(i[0], 11)
|
self.assertEqual(i[0], 11)
|
||||||
|
|
||||||
|
def testReschedule(self):
|
||||||
|
sched = schedule.Schedule()
|
||||||
|
i = [0]
|
||||||
|
def inc():
|
||||||
|
i[0] += 1
|
||||||
|
n = sched.addEvent(inc, time.time() + 1)
|
||||||
|
sched.rescheduleEvent(n, time.time() + 3)
|
||||||
|
time.sleep(1.2)
|
||||||
|
sched.run()
|
||||||
|
self.assertEqual(i[0], 0)
|
||||||
|
time.sleep(2)
|
||||||
|
sched.run()
|
||||||
|
self.assertEqual(i[0], 1)
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user