[英]Python - how to kill the timer when necessary instead of let it run forever?
How do i kill this timer, once it was executed/started ? 一旦它被执行/启动,我如何杀死这个计时器?
def my_timer(*args):
return True# do ur work here, but not for long
gtk.timeout_add(1000, my_timer) # call every min
Two options: 两种选择:
g_source_remove(event_id)
when it's no longer needed g_source_remove(event_id)
Also, the "call every minute" comment is wrong: the handle will be called every second. 此外,“每分钟调用”注释是错误的:句柄将每秒调用一次。
Suggestion: use timeout_add_seconds() if you do not need sub-second accuracy. 建议:如果您不需要亚秒精度,请使用timeout_add_seconds()。 It allows glib to optimize things and is better for power management.
它允许glib优化事物,更好地进行电源管理。
def my_timer(*args):
return True# do ur work here, but not for long
t =gtk.timeout_add(1000, my_timer) # call every min
time.sleep(5)
gtk.timeout_remove(t) # kill the timer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.