How can I make this timer run forever?

十年热恋 提交于 2020-01-13 11:12:13

问题


from threading import Timer

def hello():
    print "hello, world"

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

This code only fires the timer once.

How can I make the timer run forever?

Thanks,

updated

this is right :

import time,sys

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

and this:

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

回答1:


A threading.Timer executes a function once. That function can "run forever" if you wish, for example:

import time

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

Using multiple Timer instances would consume substantial resources with no real added value. If you want to be non-invasive to the function you're repeating every 30 seconds, a simple way would be:

import time

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

and then schedule makerepeater(30, hello) instead of hello.

For more sophisticated operations, I recommend standard library module sched.




回答2:


Just restart (or recreate) the timer within the function:

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



回答3:


from threading import Timer it depends on which part you want to run for ever, if it's creating a new thread let's say every 10 seconds you can do the following from threading import Timer

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

if it's the hello world you want to run forever you can do the following:

from threading import Timer

def hello():
    while True: # Runs this part forever
        print "hello, world"

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

Search up loops in python to get more info on this




回答4:


This is my code for this problem:

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)

Another script page:

def doIt(arguments):
    #what do you do
    for argument in arguments:
        print(argument)

mypTimer=pTimer(2,doIt,"argu 1","argu 2",5)

mypTimer.start()


来源:https://stackoverflow.com/questions/2702890/how-can-i-make-this-timer-run-forever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!