How to unset a timer set by python gobject.timeout_add ? threads?

六月ゝ 毕业季﹏ 提交于 2019-12-10 16:17:01

问题


I want to set timer in a thread and kill that thread when I want to unset the timer set by gobject.timeout_add, is this a good way to do this?

basically I want to run a function for every 180 seconds but I want to be able to stop it whenever I want to(called from another function). How to achieve this properly?

I have read that killing a thread is bad! How bad is it for simple tasks like this?


回答1:


According to the docs when you call gobject.timeout_add it returns a int which is unique for that timeout source. And then also farther down in the docs, you see a function called gobject.source_remove which takes, as an argument, an int that will remove the event source for you without having to mess with threads and the like. Example:

integer_id = gobject.timeout_add( 180000, callback_func)
#And then somewhere else in your code...
gobject.source_remove(integer_id) #This will stop the timeout_add from occurring!

Hope that helps!



来源:https://stackoverflow.com/questions/10829341/how-to-unset-a-timer-set-by-python-gobject-timeout-add-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!