本文整理汇总了Python中scheduler.Scheduler.add_timer方法的典型用法代码示例。如果您正苦于以下问题:Python Scheduler.add_timer方法的具体用法?Python Scheduler.add_timer怎么用?Python Scheduler.add_timer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scheduler.Scheduler
的用法示例。
在下文中一共展示了Scheduler.add_timer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_scheduler_timed_listener
# 需要导入模块: from scheduler import Scheduler [as 别名]
# 或者: from scheduler.Scheduler import add_timer [as 别名]
def test_scheduler_timed_listener(self):
scheduler = Scheduler()
listener = SchedulerTestListener('test_event')
scheduler.add_listener(listener)
scheduler.add_timer(0.250,'test_event')
start = time()
count = 0
while time() < start+1.05:
event = scheduler.schedule()
if event!='idle' and event.label == 'test_event':
count = count + 1
sleep(0.05)
self.assertEqual(count,4)
self.assertEqual(listener.count,4)
示例2: SchedulerTest
# 需要导入模块: from scheduler import Scheduler [as 别名]
# 或者: from scheduler.Scheduler import add_timer [as 别名]
class SchedulerTest(unittest.TestCase):
def test_scheduler_schedule(self):
self.scheduler = Scheduler()
self.assertEqual(self.scheduler.schedule(),'idle')
def test_scheduler_insert_event(self):
scheduler = Scheduler()
t = time()
while time() < t+1:
self.assertEqual(scheduler.schedule(),'idle')
sleep(0.05)
event = Event(time(), 'test_event')
self.assertEqual(scheduler.schedule([event]),event)
def test_scheduler_add_timer(self):
self.scheduler = Scheduler()
self.scheduler.add_timer(2, 'test_timer')
self.assertEqual(len(self.scheduler.timers),1);
self.assertTrue('test_timer' in self.scheduler.timers.keys())
self.assertEqual(self.scheduler.timers['test_timer'].getPeriod(),2)
def test_scheduler_timer(self):
self.scheduler = Scheduler()
error = 0.1
delay = 2
startTime = time()
self.scheduler.add_timer(delay,'test_timer')
while time() < startTime+delay-error:
self.assertEqual(self.scheduler.schedule(),'idle')
sleep(error/10)
event_count = 0
while time() < startTime+delay+error:
event = self.scheduler.schedule()
if event!='idle' and event.label=='test_timer':
event_count = event_count + 1
sleep(error/10)
self.assertEqual(event_count,1)
def test_scheduler_add_listener(self):
self.scheduler = Scheduler()
listener = SchedulerTestListener('foo')
self.scheduler.add_listener(listener)
self.assertEqual(len(self.scheduler.listeners),1)
self.assertEqual(self.scheduler.listeners['foo'],[listener])
def test_scheduler_add_bad_listener(self):
self.scheduler = Scheduler()
listener = SchedulerTestListener('foo')
listener.addEvent(None)
type_error = False
try:
self.scheduler.add_listener(listener)
except TypeError:
type_error = True
self.assertEqual(type_error,True)
def test_scheduler_listener(self):
scheduler = Scheduler()
listener = SchedulerTestListener('test_event')
scheduler.add_listener(listener)
self.assertEqual(scheduler.schedule(),'idle')
event = Event(time(),'test_event')
self.assertEqual(scheduler.schedule([event]),event)
self.assertEqual(listener.count,1)
def test_scheduler_timed_listener(self):
scheduler = Scheduler()
listener = SchedulerTestListener('test_event')
scheduler.add_listener(listener)
scheduler.add_timer(0.250,'test_event')
start = time()
#.........这里部分代码省略.........