views:

94

answers:

5

Hi guys,

I want to know about timer in Python.

Suppose i have a code snippet something like:

def abc()
   print 'Hi'  
   print 'Hello'
   print 'Hai'

And i want to print it every 1 second. Max three times;ie; 1st second i need to check the printf, 2nd second I need to check as well in 3rd second.

In my actual code variables value will be updated. I need to capture at what second all the variables are getting updated.

Can anybody tell me how to do this.

A: 

You should look into time.sleep(). For example:

for i in xrange(5):
  abc()
  time.sleep(3)

That will print your lines 5 times with a 3 second delay between.

g.d.d.c
A: 
import time
def abc()
    for i in range(3):
        print 'Hi'  
        print 'Hello'
        print 'Hai'
        time.sleep(1)
Steven Rumbalski
+2  A: 

Use time.sleep.

import time

def abc():
    print 'Hi'
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    time.sleep(1)
    abc()   
Chris B.
within 1 second I need to print. In the above program after giving 1 sec delay its been printed. I want it within 1 sec.My program will contain variables which will be getting updated. What I want is to monitor how many seconds it is taking for all the variables to get updated.
A: 
import time
def abc():
 print 'Hi'
 print 'Hello'
 print 'Hai'

for i in range(3):
 time.sleep(3-i)
 abc()
foret
A: 

time.sleep is fine in this case but what is the abc() function takes half a second to execute? Or 5 minutes? In this case you should use a Timer object.

from threading import Timer

def abc()
    print 'Hi'  
    print 'Hello'
    print 'Hai'

for i in [1,2,3]:
    Timer(i, abc).start()
Joss82