sta*_*mat 29
这可能是您正在寻找的正确代码段:
import threading
def set_interval(func, sec):
    def func_wrapper():
        set_interval(func, sec)
        func()
    t = threading.Timer(sec, func_wrapper)
    t.start()
    return t
只是保持简洁,我不知道为什么所有的答案都让事情变得如此复杂:
import threading
def setInterval(func,time):
    e = threading.Event()
    while not e.wait(time):
        func()
def foo():
    print "hello"
# using
setInterval(foo,5)
# output:
hello
hello
.
.
.
编辑:此代码是非阻塞的
import threading
class ThreadJob(threading.Thread):
    def __init__(self,callback,event,interval):
        '''runs the callback function after interval seconds
        :param callback:  callback function to invoke
        :param event: external event for controlling the update operation
        :param interval: time in seconds after which are required to fire the callback
        :type callback: function
        :type interval: int
        '''
        self.callback = callback
        self.event = event
        self.interval = interval
        super(ThreadJob,self).__init__()
    def run(self):
        while not self.event.wait(self.interval):
            self.callback()
event = threading.Event()
def foo():
    print "hello"
k = ThreadJob(foo,event,2)
k.start()
print "It is non-blocking"
这是一个可以启动和停止的版本.它没有阻止.没有故障,因为没有添加执行时间错误(对于长时间执行很重要,例如音频间隔非常短)
import time, threading
StartTime=time.time()
def action() :
    print('action ! -> time : {:.1f}s'.format(time.time()-StartTime))
class setInterval :
    def __init__(self,interval,action) :
        self.interval=interval
        self.action=action
        self.stopEvent=threading.Event()
        thread=threading.Thread(target=self.__setInterval)
        thread.start()
    def __setInterval(self) :
        nextTime=time.time()+self.interval
        while not self.stopEvent.wait(nextTime-time.time()) :
            nextTime+=self.interval
            self.action()
    def cancel(self) :
        self.stopEvent.set()
# start action every 0.6s
inter=setInterval(0.6,action)
print('just after setInterval -> time : {:.1f}s'.format(time.time()-StartTime))
# will stop interval in 5s
t=threading.Timer(5,inter.cancel)
t.start()
输出是:
just after setInterval -> time : 0.0s
action ! -> time : 0.6s
action ! -> time : 1.2s
action ! -> time : 1.8s
action ! -> time : 2.4s
action ! -> time : 3.0s
action ! -> time : 3.6s
action ! -> time : 4.2s
action ! -> time : 4.8s
我想这就是你所追求的:
#timertest.py
import sched, time
def dostuff():
  print "stuff is being done!"
  s.enter(3, 1, dostuff, ())
s = sched.scheduler(time.time, time.sleep)
s.enter(3, 1, dostuff, ())
s.run()
如果您在重复方法的末尾添加另一个条目到调度程序,它就会继续进行。
改变Nailxx的答案,你就得到了答案!
from threading import Timer
def hello():
    print "hello, world"
    Timer(30.0, hello).start()
Timer(30.0, hello).start() # after 30 seconds, "hello, world" will be printed
import functools
import sched, time
s = sched.scheduler(time.time, time.sleep)
def setInterval(sec):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*argv, **kw):
            setInterval(sec)(func)
            func(*argv, **kw)
        s.enter(sec, 1, wrapper, ())
        return wrapper
    s.run()
    return decorator
@setInterval(sec=3)
def testInterval():
  print ("test Interval ")
testInterval()
| 归档时间: | 
 | 
| 查看次数: | 35456 次 | 
| 最近记录: |