Python等效的setInterval()?

时间:2010-04-23 08:02:30

标签: python setinterval

Python是否具有与JavaScript的setInterval()类似的功能?

由于

20 个答案:

答案 0 :(得分:27)

这可能是您正在寻找的正确代码段:

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

答案 1 :(得分:7)

保持简单明了。

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"

答案 2 :(得分:7)

这是一个可以启动和停止的版本。 它没有阻止。 没有故障,因为没有添加执行时间错误(对于长时间执行非常重要,例如音频的间隔非常短)

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

答案 3 :(得分:5)

稍微更改 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

答案 4 :(得分:5)

sched模块为常规Python代码提供了这些功能。但是,正如其文档所示,如果您的代码是多线程的,那么使用threading.Timer类可能更有意义。

答案 5 :(得分:3)

我认为这就是你所追求的:

#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()

如果在重复方法结束时向调度程序添加另一个条目,它将继续运行。

答案 6 :(得分:2)

我使用sched创建setInterval函数gist

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()

答案 7 :(得分:1)

简单的setInterval实用程序

from threading import Timer

def setInterval(timer, task):
    isStop = task()
    if not isStop:
        Timer(timer, setInterval, [timer, task]).start()

def hello():
    print "do something"
    return False # return True if you want to stop

if __name__ == "__main__":
    setInterval(2.0, hello) # every 2 seconds, "do something" will be printed

答案 8 :(得分:0)

我的Python 3模块jsinterval.py会有所帮助!这是:

"""
Threaded intervals and timeouts from JavaScript
"""

import threading, sys

__all__ =  ['TIMEOUTS', 'INTERVALS', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout']

TIMEOUTS  = {}
INTERVALS = {}

last_timeout_id  = 0
last_interval_id = 0

class Timeout:
    """Class for all timeouts."""
    def __init__(self, func, timeout):
        global last_timeout_id
        last_timeout_id += 1
        self.timeout_id = last_timeout_id
        TIMEOUTS[str(self.timeout_id)] = self
        self.func = func
        self.timeout = timeout
        self.threadname = 'Timeout #%s' %self.timeout_id

    def run(self):
        func = self.func
        delx = self.__del__
        def func_wrapper():
            func()
            delx()
        self.t = threading.Timer(self.timeout/1000, func_wrapper)
        self.t.name = self.threadname
        self.t.start()

    def __repr__(self):
        return '<JS Timeout set for %s seconds, launching function %s on timeout reached>' %(self.timeout, repr(self.func))

    def __del__(self):
        self.t.cancel()

class Interval:
    """Class for all intervals."""
    def __init__(self, func, interval):
        global last_interval_id
        self.interval_id = last_interval_id
        INTERVALS[str(self.interval_id)] = self
        last_interval_id += 1
        self.func = func
        self.interval = interval
        self.threadname = 'Interval #%s' %self.interval_id

    def run(self):
        func = self.func
        interval = self.interval
        def func_wrapper():
            timeout = Timeout(func_wrapper, interval)
            self.timeout = timeout
            timeout.run()
            func()
        self.t = threading.Timer(self.interval/1000, func_wrapper)
        self.t.name = self.threadname
        self.t.run()

    def __repr__(self):
        return '<JS Interval, repeating function %s with interval %s>' %(repr(self.func), self.interval)

    def __del__(self):
        self.timeout.__del__()

def setInterval(func, interval):
    """
    Create a JS Interval: func is the function to repeat, interval is the interval (in ms)
    of executing the function.
    """
    temp = Interval(func, interval)
    temp.run()
    idx = int(temp.interval_id)
    del temp
    return idx


def clearInterval(interval_id):
    try:
        INTERVALS[str(interval_id)].__del__()
        del INTERVALS[str(interval_id)]
    except KeyError:
        sys.stderr.write('No such interval "Interval #%s"\n' %interval_id)

def setTimeout(func, timeout):
    """
    Create a JS Timeout: func is the function to timeout, timeout is the timeout (in ms)
    of executing the function.
    """
    temp = Timeout(func, timeout)
    temp.run()
    idx = int(temp.timeout_id)
    del temp
    return idx


def clearTimeout(timeout_id):
    try:
        TIMEOUTS[str(timeout_id)].__del__()
        del TIMEOUTS[str(timeout_id)]
    except KeyError:
        sys.stderr.write('No such timeout "Timeout #%s"\n' %timeout_id)

代码编辑: 修复了内存泄漏(由@benjaminz发现)。现在所有线程都被清理干净了。为什么会发生这种泄漏?它发生是因为隐式(甚至是显式)引用。就我而言,TIMEOUTSINTERVALS。超时自动清理(在此补丁之后),因为它们使用函数包装器调用函数然后自我杀死。但这是怎么发生的?除非删除所有引用或使用gc模块,否则无法从内存中删除对象。解释:没有办法(在我的代码中)创建对超时/间隔的不需要的引用。他们只有一个推荐人:TIMEOUTS / INTERVALS。并且,当中断或完成时(只有超时可以不间断地完成),它们会删除对自己的唯一现有引用:它们对应的dict元素。使用__all__完全封装了类,因此没有内存泄漏的空间。

答案 9 :(得分:0)

这里有些简单的话:

GridField

答案 10 :(得分:0)

入睡,直到下一个间隔seconds的长度开始:(不并发)

def sleep_until_next_interval(self, seconds):
    now = time.time()
    fall_asleep = seconds - now % seconds
    time.sleep(fall_asleep)

while True:
    sleep_until_next_interval(10) # 10 seconds - worktime
    # work here

简单无漂移。

答案 11 :(得分:0)

我已经编写了代码,以便在python中创建非常非常灵活的setInterval。您在这里:

import threading


class AlreadyRunning(Exception):
    pass


class IntervalNotValid(Exception):
    pass


class setInterval():
    def __init__(this, func=None, sec=None, args=[]):
        this.running = False
        this.func = func  # the function to be run
        this.sec = sec            # interval in second
        this.Return = None  # The returned data
        this.args = args
        this.runOnce = None  # asociated with run_once() method
        this.runOnceArgs = None   # asociated with run_once() method

        if (func is not None and sec is not None):
            this.running = True

            if (not callable(func)):
                raise TypeError("non-callable object is given")

            if (not isinstance(sec, int) and not isinstance(sec, float)):
                raise TypeError("A non-numeric object is given")

            this.TIMER = threading.Timer(this.sec, this.loop)
            this.TIMER.start()

    def start(this):
        if (not this.running):
            if (not this.isValid()):
                raise IntervalNotValid("The function and/or the " +
                                       "interval hasn't provided or invalid.")
            this.running = True
            this.TIMER = threading.Timer(this.sec, this.loop)
            this.TIMER.start()
        else:
            raise AlreadyRunning("Tried to run an already run interval")

    def stop(this):
        this.running = False

    def isValid(this):
        if (not callable(this.func)):
            return False

        cond1 = not isinstance(this.sec, int)
        cond2 = not isinstance(this.sec, float)
        if (cond1 and cond2):
            return False
        return True

    def loop(this):

        if (this.running):
            this.TIMER = threading.Timer(this.sec, this.loop)
            this.TIMER.start()
            function_, Args_ = this.func, this.args

            if (this.runOnce is not None):  # someone has provide the run_once
                runOnce, this.runOnce = this.runOnce, None
                result = runOnce(*(this.runOnceArgs))
                this.runOnceArgs = None

                # if and only if the result is False. not accept "None"
                # nor zero.
                if (result is False):
                    return  # cancel the interval right now

            this.Return = function_(*Args_)

    def change_interval(this, sec):

        cond1 = not isinstance(sec, int)
        cond2 = not isinstance(sec, float)
        if (cond1 and cond2):
            raise TypeError("A non-numeric object is given")

        # prevent error when providing interval to a blueprint
        if (this.running):
            this.TIMER.cancel()

        this.sec = sec

        # prevent error when providing interval to a blueprint
        # if the function hasn't provided yet
        if (this.running):
            this.TIMER = threading.Timer(this.sec, this.loop)
            this.TIMER.start()

    def change_next_interval(this, sec):

        if (not isinstance(sec, int) and not isinstance(sec, float)):
            raise TypeError("A non-numeric object is given")

        this.sec = sec

    def change_func(this, func, args=[]):

        if (not callable(func)):
            raise TypeError("non-callable object is given")

        this.func = func
        this.args = args

    def run_once(this, func, args=[]):
        this.runOnce = func
        this.runOnceArgs = args

    def get_return(this):
        return this.Return

您可以获得许多功能和灵活性。运行此代码不会冻结代码,您可以在运行时更改时间间隔,可以在运行时更改函数,可以传递参数,可以从函数中获取返回的对象,等等。您也可以自己捣蛋!

这是一个非常简单的基本示例:

import time

def interval(name="world"):
  print(f"Hello {name}!")

# function named interval will be called every two seconds
# output: "Hello world!"
interval1 = setInterval(interval, 2) 

# function named interval will be called every 1.5 seconds
# output: "Hello Jane!"
interval2 = setInterval(interval, 1.5, ["Jane"]) 

time.sleep(5) #stop all intervals after 5 seconds
interval1.stop()
interval2.stop()

查看我的Github项目,以查看更多示例并关注后续更新:D https://github.com/Hzzkygcs/setInterval-python

答案 12 :(得分:0)

在上述解决方案中,如果出现程序关闭的情况,则不能保证它会正常关闭,它总是建议通过软终止来关闭程序,它们大多数都没有停止功能Sankalp写的一篇很好的关于媒介的文章,解决了这两个问题(run periodic tasks in python),请参考附件链接以获取更深入的了解。   在以下示例中,使用了一个名为signal的库来跟踪杀死是软杀死还是硬杀死

import threading, time, signal

from datetime import timedelta

WAIT_TIME_SECONDS = 1

class ProgramKilled(Exception):
    pass

def foo():
    print time.ctime()

def signal_handler(signum, frame):
    raise ProgramKilled

class Job(threading.Thread):
    def __init__(self, interval, execute, *args, **kwargs):
        threading.Thread.__init__(self)
        self.daemon = False
        self.stopped = threading.Event()
        self.interval = interval
        self.execute = execute
        self.args = args
        self.kwargs = kwargs

    def stop(self):
                self.stopped.set()
                self.join()
    def run(self):
            while not self.stopped.wait(self.interval.total_seconds()):
                self.execute(*self.args, **self.kwargs)

if __name__ == "__main__":
    signal.signal(signal.SIGTERM, signal_handler)
    signal.signal(signal.SIGINT, signal_handler)
    job = Job(interval=timedelta(seconds=WAIT_TIME_SECONDS), execute=foo)
    job.start()

    while True:
          try:
              time.sleep(1)
          except ProgramKilled:
              print "Program killed: running cleanup code"
              job.stop()
              break
#output
#Tue Oct 16 17:47:51 2018
#Tue Oct 16 17:47:52 2018
#Tue Oct 16 17:47:53 2018
#^CProgram killed: running cleanup code

答案 13 :(得分:0)

以上大多数答案未正确关闭线程。在使用Jupyter笔记本时,我注意到当发送显式中断时,线程仍在运行,更糟糕的是,它们将继续从运行1、2、4等的线程开始相乘。我的以下方法基于@doom的答案,但干净利落通过在主线程中运行无限循环来侦听SIGINT和SIGTERM事件来处理中断

  • 没有漂移
  • 可取消
  • 很好地处理SIGINT和SIGTERM
  • 不为每次运行创建新线程

随时提出改进建议

import time
import threading
import signal

# Record the time for the purposes of demonstration 
start_time=time.time()

class ProgramKilled(Exception):
    """
    An instance of this custom exception class will be thrown everytime we get an SIGTERM or SIGINT
    """
    pass

# Raise the custom exception whenever SIGINT or SIGTERM is triggered
def signal_handler(signum, frame):
    raise ProgramKilled

# This function serves as the callback triggered on every run of our IntervalThread
def action() :
    print('action ! -> time : {:.1f}s'.format(time.time()-start_time))

# https://stackoverflow.com/questions/2697039/python-equivalent-of-setinterval
class IntervalThread(threading.Thread) :
    def __init__(self,interval,action, *args, **kwargs) :
        super(IntervalThread, self).__init__()
        self.interval=interval
        self.action=action
        self.stopEvent=threading.Event()
        self.start()

    def run(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()

def main():

    # Handle SIGINT and SIFTERM with the help of the callback function
    signal.signal(signal.SIGTERM, signal_handler)
    signal.signal(signal.SIGINT, signal_handler)
    # start action every 1s
    inter=IntervalThread(1,action)
    print('just after setInterval -> time : {:.1f}s'.format(time.time()-start_time))

    # will stop interval in 500s
    t=threading.Timer(500,inter.cancel)
    t.start()

    # https://www.g-loaded.eu/2016/11/24/how-to-terminate-running-python-threads-using-signals/
    while True:
        try:
            time.sleep(1)
        except ProgramKilled:
            print("Program killed: running cleanup code")
            inter.cancel()
            break

if __name__ == "__main__":
    main()

答案 14 :(得分:0)

这是一个低时间漂移解决方案,它使用线程定期发出Event对象的信号。线程的run()在等待超时时几乎不做任何事情;因此时间漂移很短。

# Example of low drift (time) periodic execution of a function.
import threading
import time

# Thread that sets 'flag' after 'timeout'
class timerThread (threading.Thread):

    def __init__(self , timeout , flag):
        threading.Thread.__init__(self)
        self.timeout = timeout
        self.stopFlag = False
        self.event = threading.Event()
        self.flag = flag

    # Low drift run(); there is only the 'if'
    # and 'set' methods between waits.
    def run(self):
        while not self.event.wait(self.timeout):
            if self.stopFlag:
                break
            self.flag.set()

    def stop(self):
        stopFlag = True
        self.event.set()

# Data.
printCnt = 0

# Flag to print.
printFlag = threading.Event()

# Create and start the timer thread.
printThread = timerThread(3 , printFlag)
printThread.start()

# Loop to wait for flag and print time.
while True:

    global printCnt

    # Wait for flag.
    printFlag.wait()
    # Flag must be manually cleared.
    printFlag.clear()
    print(time.time())
    printCnt += 1
    if printCnt == 3:
        break;

# Stop the thread and exit.
printThread.stop()
printThread.join()
print('Done')

答案 15 :(得分:0)

上面的一些答案使用func_wrapperthreading.Timer确实有效,除了它每次调用一个间隔时产生一个新线程,这会导致内存问题。

下面的基本示例粗略通过将区间放在单独的线程上来实现类似的机制。它以给定的间隔睡觉。在跳转到代码之前,您需要注意以下一些限制:

  1. JavaScript是单线程的,所以当setInterval中的函数被触发时,其他任何东西都不会同时工作(不包括工作线程,但让我们来谈谈{的一般用例{1}}。因此,线程是安全的。但是在此实现中,除非使用setInterval,否则您可能会遇到竞争条件。

  2. 下面的实现使用threading.rLock来模拟间隔,但添加time.sleep的执行时间,此间隔的总时间可能会大于您的期望。因此,根据使用情况,您可能希望“少睡一觉”。 (减去调用func所花费的时间)

  3. 我只是粗略测试了这个,你绝对不应该像我那样使用全局变量,随意调整它以适应你的系统。

  4. 说够了,这是代码:

    func

    预期产出:

    # Python 2.7
    import threading
    import time
    
    
    class Interval(object):
        def __init__(self):
            self.daemon_alive = True
            self.thread = None # keep a reference to the thread so that we can "join"
    
        def ticktock(self, interval, func):
            while self.daemon_alive:
                time.sleep(interval)
                func()
    
    num = 0
    def print_num():
        global num
        num += 1
        print 'num + 1 = ', num
    
    def print_negative_num():
        global num
        print '-num = ', num * -1
    
    intervals = {} # keep track of intervals
    g_id_counter = 0 # roughly generate ids for intervals
    
    def set_interval(interval, func):
        global g_id_counter
    
        interval_obj = Interval()
        # Put this interval on a new thread
        t = threading.Thread(target=interval_obj.ticktock, args=(interval, func))
        t.setDaemon(True)
        interval_obj.thread = t
        t.start()
    
        # Register this interval so that we can clear it later
        # using roughly generated id
        interval_id = g_id_counter
        g_id_counter += 1
        intervals[interval_id] = interval_obj
    
        # return interval id like it does in JavaScript
        return interval_id
    
    def clear_interval(interval_id):
        # terminate this interval's while loop
        intervals[interval_id].daemon_alive = False
        # kill the thread
        intervals[interval_id].thread.join()
        # pop out the interval from registry for reusing
        intervals.pop(interval_id)
    
    if __name__ == '__main__':
        num_interval = set_interval(1, print_num)
        neg_interval = set_interval(3, print_negative_num)
    
        time.sleep(10) # Sleep 10 seconds on main thread to let interval run
        clear_interval(num_interval)
        clear_interval(neg_interval)
        print "- Are intervals all cleared?"
        time.sleep(3) # check if both intervals are stopped (not printing)
        print "- Yup, time to get beers"
    

答案 16 :(得分:0)

上面的方法并不适合我,因为我需要能够取消间隔。我把这个功能变成了一个类,并提出了以下内容:

EAGAIN/EWOULDBLOCK

答案 17 :(得分:0)

最近,我和你有同样的问题。我找到了这些解决方案:

1。您可以使用该库:threading.Time(这已在上面介绍)

2。你可以使用这个库:sched(这也有上面的介绍)

第3。您可以使用该库:Advanced Python Scheduler (推荐)

答案 18 :(得分:-1)

来自Python Documentation

from threading import Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

答案 19 :(得分:-1)

Python中的工作方式不同:您需要sleep()(如果要阻止当前线程)或启动新线程。见http://docs.python.org/library/threading.html