简体   繁体   中英

How can I make this timer run forever?

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

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 .

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM