【问题标题】:How can I make this timer run forever?我怎样才能让这个计时器永远运行?
【发布时间】:2010-04-24 01:54:02
【问题描述】:
from threading import Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start()

此代码只触发一次计时器。

如何让计时器永远运行?

谢谢,

更新

这是对的:

import time,sys

def hello():
    while True:
        print "Hello, Word!"
        sys.stdout.flush()
        time.sleep(2.0)
hello()

还有这个:

from threading import Timer

def hello():
    print "hello, world"
    sys.stdout.flush()
    t = Timer(2.0, hello)
    t.start()

t = Timer(2.0, hello)
t.start()

【问题讨论】:

    标签: python multithreading timer


    【解决方案1】:

    threading.Timer 执行一个函数一次。如果您愿意,该函数可以“永远运行”,例如:

    import time
    
    def hello():
        while True:
            print "Hello, Word!"
            time.sleep(30.0)
    

    使用多个Timer 实例会消耗大量资源而没有真正的附加值。如果您想对每 30 秒重复一次的功能保持非侵入性,一个简单的方法是:

    import time
    
    def makerepeater(delay, fun, *a, **k):
        def wrapper(*a, **k):
            while True:
                fun(*a, **k)
                time.sleep(delay)
        return wrapper
    

    然后安排makerepeater(30, hello) 而不是hello

    对于更复杂的操作,我推荐标准库模块sched

    【讨论】:

    • 但我看不到打印任何东西
    • 嗨,亚历克斯,你知道如何在剧院里做这个吗..谢谢
    • print 来自线程(假设这就是您所说的“therad”)可能不是一个好主意——logging 模块保证是线程安全的,print 是不。无论如何,如果你用Timer 调度makerepeater 的结果,它当然会(尝试)在自己的线程中运行。
    【解决方案2】:

    只需在函数内重新启动(或重新创建)计时器:

    #!/usr/bin/python
    from threading import Timer
    
    def hello():
        print "hello, world"
        t = Timer(2.0, hello)
        t.start()
    
    t = Timer(2.0, hello)
    t.start()
    

    【讨论】:

    • 我知道,对吧?似乎这个问题的每个答案都被否决了,没有任何解释。
    • 作者或其他人似乎在拖钓:/
    • 这对我不起作用:raise RuntimeError("thread already started")
    • @zoodbergi,不确定你做错了什么,在 2.7.3 和 3.2.3 中工作正常(一旦括号添加到 print)。没有引发异常。
    • 似乎无法重新启动 一个现有的 Timer,但是然后将您的标识符 t 重新绑定到一个 new Timer开始 is 是可能的,这就是这个例子所展示的。我假设@zoidbergi 您没有创建新计时器,因此出现错误。 (根据文档,TimerThread 的子类。)
    【解决方案3】:

    从线程导入定时器 这取决于你想永远运行哪个部分,如果它正在创建一个新线程,假设每 10 秒你可以执行以下操作 从线程导入定时器

    import time
    def hello():
        print "hello, world"
    
    while True: #Runs the code forever over and over again, called a loop
        time.sleep(10)#Make it sleep 10 seconds, as to not create too many threads
        t = Timer(30.0, hello)
        t.start()
    

    如果它是你想要永远运行的 hello world,你可以执行以下操作:

    from threading import Timer
    
    def hello():
        while True: # Runs this part forever
            print "hello, world"
    
    t = Timer(30.0, hello)
    t.start()
    

    在 python 中搜索循环以获取更多信息

    【讨论】:

      【解决方案4】:

      这是我解决这个问题的代码:

      import time
      from  threading import Timer
      
      class pTimer():
          def __init__(self,interval,handlerFunction,*arguments):
              self.interval=interval
              self.handlerFunction=handlerFunction
              self.arguments=arguments
              self.running=False
              self.timer=Timer(self.interval,self.run,arguments)
      
          def start(self):
              self.running=True
              self.timer.start()
              pass
      
          def stop(self):
              self.running=False
              pass
      
          def run(self,*arguments):
              while self.running:
                 self.handlerFunction(arguments)
                 time.sleep(self.interval)
      

      另一个脚本页面:

      def doIt(arguments):
          #what do you do
          for argument in arguments:
              print(argument)
      
      mypTimer=pTimer(2,doIt,"argu 1","argu 2",5)
      
      mypTimer.start()
      

      【讨论】:

        猜你喜欢
        • 2014-08-11
        • 1970-01-01
        • 1970-01-01
        • 2020-10-22
        • 1970-01-01
        • 2021-05-27
        • 2021-02-17
        • 2010-12-10
        • 1970-01-01
        相关资源
        最近更新 更多