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....

Recommended Answers

All 3 Replies

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.

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?

Here's a very basic example:

import threading
import time

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

t = threading.Thread(target=handler)

for i in range(20):
    print "hello!"

handler() will chime in every 5 seconds.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.