如何取消设置由python gobject.timeout_add设置的计时器?线程? [英] How to unset a timer set by python gobject.timeout_add ? threads?

查看:132
本文介绍了如何取消设置由python gobject.timeout_add设置的计时器?线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在线程中设置计时器,并在我想取消设置由gobject.timeout_add设置的计时器时杀死该线程,这是执行此操作的好方法吗?

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?

基本上,我想每180秒运行一个函数,但是我希望能够随时停止它(从另一个函数调用).如何正确实现呢?

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?

推荐答案

根据文档,它将返回一个int,该超时源对该超时源是唯一的.然后在 docs 中走得更远,您会看到一个名为gobject.source_remove的函数,该函数将int作为参数,它将为您删除事件源,而不必弄乱线程等. 示例:

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!

希望有帮助!

这篇关于如何取消设置由python gobject.timeout_add设置的计时器?线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆