Executing periodic actions [duplicate] Ask Question

Executing periodic actions [duplicate] Ask Question

I am working on Windows. I want to execute a function foo() every 10 seconds.
How do I do this?

ベストアンサー1

At the end of foo(), create a Timer which calls foo() itself after 10 seconds.
Because, Timer create a new thread to call foo().
You can do other stuff without being blocked.

import time, threading
def foo():
    print(time.ctime())
    threading.Timer(10, foo).start()

foo()

#output:
#Thu Dec 22 14:46:08 2011
#Thu Dec 22 14:46:18 2011
#Thu Dec 22 14:46:28 2011
#Thu Dec 22 14:46:38 2011

おすすめ記事