1

On a webserver i am currently implementing in web.py , I am using the following approach to perform an action periodically:

import threading    

def periodicAction():
    # do something
    threading.Timer(time_interval, periodicAction).start() # when finished, wait and then start same function in a new thread

periodicAction() # start the method

While it works fine (meaning it does what it is supposed to do), I still have the problem, that when I test it from the command line, the console gets unresponsive (i can still type, but it has no affect, even ctrl + c does not stop the program). It this the normal behaviour or am I doing something wrong?

1 Answer 1

2

The background thread is still running, so if the main thread finishes, it will wait -- forever, in this case. (It's a side-effect of how it waits that Ctrl-C doesn't work.) If you don't want this, you can call setDaemon(True), which makes the thread a "daemon" -- meaning that it will be forcefully closed when the main thread finishes:

def periodicAction():
    print "do something"
    t = threading.Timer(1.0, periodicAction)
    t.setDaemon(True)
    t.start()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.