views:

123

answers:

5

Is there a way to, for example, print Hello World! every n seconds? For example, the program would go through whatever code I had, then once it had been 5 seconds (with time.sleep()) it would execute that code. I would be using this to update a file though, not print Hello World.

For example:

startrepeat('print('Hello World'), .01) #repeats print('Hello World') ever .01 seconds 
for i in range(5):
    print(i)

>> Hello World!
>> 0
>> 1
>> 2
>> Hello World!
>> 3
>> Hello World!
>> 4
+1  A: 
def update():
    import time
    while True:
        print 'Hello World!'
        time.sleep(5)

That'll run as a function. The while True: makes it run forever. You can always take it out of the function if you need.

vlad003
Doesn't work; it just runs forever and I cant do anything else while it is.
Zonda333
What other things do you want to be doing at the same time?
Jeremy Friesner
Well this just runs in a loop. You didn't specify in the question that you'd be doing something else in the meantime so I assumed that's what you need.
vlad003
A: 

You can start a separate thread whose sole duty is to count for 5 seconds, update the file, repeat. You wouldn't want this separate thread to interfere with your main thread.

Kit
A: 

Please read this for sample use of thread.sleep() and threading considerations (including what the GIL can do to performance, which is that it will wreck performance, keep multi-threading extremely simple and lightweight in python). This sample also demonstrates the active objects pattern of multithread processing, which I recommend highly.

marr75
+6  A: 
import threading

def printit():
  threading.Timer(5.0, printit).start()
  print "Hello, World!"

printit()

# continue with the rest of your code
Alex Martelli
This isn't working either - it will just print hello world then do whatever code I put after it, without repeating itself.
Zonda333
you can probably create a method SetAlarm which takes all require arguments to execute the function and this method will set threading.Timer and just return, I mean like this..........timer = threading.Timer(wait, fn, args, kwargs) you can also use mx DateTime to calculate the time interval using DateTime.RelativeDateTime(days=interval),timer.start()
Tumbleweed
@Zonda, oops, forgot to start the thread -- editing now to fix, sorry.
Alex Martelli
Alex Martelli
@shah, yes, the idea can be wrapped in a more general function (not necessarily a method -- personally, if I needed that, I'd make it a decorator-style nested function), though the _repeated_ calls to `threading.Timer` are a must, be they directly in the function that must repeat, or a smart general wrapper for it. `mx.DateTime` is pretty obsolete since Python's standard library gained module `datetime` (many years ago) and wouldn't help anyway for the "repeat every 5 seconds" spec the OP posed in their question.
Alex Martelli
A: 
from threading import Thread
import time

def printHelloWorld(n):
    while True:
        print('Hello world...')
        time.sleep(n)

t = Thread(target=printHelloWorld, args=(.01, ))
t.run()
Artur Gaspar