本文介绍了如何在Python异步执行的函数每60秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要执行Python的每60秒一个功能,但我不希望被阻止同时
I want to execute a function every 60 seconds on Python but I don't want to be blocked meanwhile.
我该怎么办呢异步?
import threading
import time
def f():
print("hello world")
threading.Timer(3, f).start()
if __name__ == '__main__':
f()
time.sleep(20)
使用此code时,函数f每3秒执行20秒选定了time.time内。
在结束它提供了一个错误,我认为,这是因为threading.timer并没有被取消。
With this code, the function f is executed every 3 seconds within the 20 seconds time.time.At the end it gives an error and I think that it is because the threading.timer has not been canceled.
我怎样才能取消?
在此先感谢!
推荐答案
您可以尝试threading.Timer类:的。
You could try the threading.Timer class: http://docs.python.org/library/threading.html#timer-objects.
import threading
def f():
# do something here ...
# call f() again in 60 seconds
threading.Timer(60, f).start()
# start calling f now and every 60 sec thereafter
f()
这篇关于如何在Python异步执行的函数每60秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!