0

i am a beginner ,just started using python from 1 week.....

without using time.sleep()...
i want a handler to call a function whenever timer of 1 sec expires repeatively....

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by jrcagle
0

Normally I would accomplish this with a separate "timer thread" that looks for time to pass and calls the requested function when appropriate. In that case I'd have the timer thread sleep for a second, call the function, lather, rinse, repeat...

Why do you not want to use time.sleep()? Do you have other things to do? But that's what threading is all about: having individual threads perform tasks in parallel when needed. While one thread sleeps, the other one can still work.

0

exactly as u said , at the same i need to do other things parallel... time.sleep(1) waits for 1 second ... i want negate that waiting .... something like each time 1 second timer expires a handler should be called.... is there any interrupt action in python?

0

Here's a very basic example:

import threading
import time

def handler():
    for i in range(5):
        time.sleep(5)
        print "Hi!"

t = threading.Thread(target=handler)
t.start()

for i in range(20):
    print "hello!"
    time.sleep(1)

handler() will chime in every 5 seconds.

Jeff

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.