views:

282

answers:

4
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: 

A Timer executes code only once. You need to start another one over and over again if you want it to run several times:

import threading

def hello():
    print "hello"
    t = threading.Timer(30, hello)
    t.start()

t = threading.Timer(30, hello)
t.start()
zneak
... aaand thanks to whoever downvoted it without telling why.
zneak
i can't see print anything
zjm1126
That's because apparently the `print` doesn't get flushed out in this case, after adding a `sys.stdout.flush()` after it the hello displayed just nice.
Ivo Wetzel
no,i also can't see when i add 'sys.stdout.flush()' after print "hello"
zjm1126
+4  A: 

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.

Alex Martelli
but i can't see print anything
zjm1126
hi alex, and did you know how to make this in a therad ..thanks
zjm1126
`print` from a thread (assuming that's what you mean by "therad") may not be a great idea -- the `logging` module is guaranteed to be thread-safe, `print` isn't. Anyway, if you schedule the result of `makerepeater` with `Timer`, it will of course (try to) run in its own thread.
Alex Martelli
A: 

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()
paxdiablo
Why, the downvote? This works fine, as per the spec.
paxdiablo
I know, right? It seems every answer to this question was downvoted without any explanation.
zneak
Author or someone else seems to be trolling :/
A: 

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

i can't see anything.
zjm1126