4 examples of 'python set timer' in Python

Every line of 'python set timer' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
257def timer_cb(self, timer):
258 """Timer callback. Update all our clocks."""
259 dt_now = datetime.utcnow().replace(tzinfo=tz.UTC)
260 self.logger.debug("timer fired. utc time is '%s'" % (str(dt_now)))
261
262 for clock in self.clocks.values():
263 clock.update_clock(dt_now)
264
265 # update clocks approx every second
266 timer.start(1.0)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

85def set(self, interval, callback, repeat=False, force=True):
86 """
87 添加timer
88 """
89 if self.timer:
90 if force:
91 # 如果已经存在,那么先要把现在的清空
92 self.clear()
93 else:
94 # 已经存在的话,就返回了
95 return
96
97 def callback_wrapper():
98 # 必须要确定,这次调用就是这个timer引起的
99 if self.timer == timer:
100 # 必须加这句,否则如果在callback中有clear操作,会出现GreenletExit
101 self.timer = None
102 # 不可以加 timer = None,否则会导致判断self.timer == timer 报错找不到timer
103 result = safe_call(callback)
104 if repeat and not self.timer:
105 # 之所以还要判断timer,是因为callback中可能设置了新的回调
106 self.set(interval, callback, repeat, True)
107 return result
108
109 self.timer = timer = gevent.spawn_later(interval, callback_wrapper)
94async def call_timer(self, timer):
95 # delete the timer
96 query = "DELETE FROM reminders WHERE id=$1;"
97 await self.bot.pool.execute(query, timer.id)
98
99 # dispatch the event
100 event_name = f'{timer.event}_timer_complete'
101 self.bot.dispatch(event_name, timer)
29def restartTimer(self):
30 self.lastTimeOnline = datetime.datetime.now().strftime("%I%M%S")

Related snippets