Created
April 2, 2013 19:25
-
-
Save lqez/5295389 to your computer and use it in GitHub Desktop.
Example of calling function every N seconds precisely.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
import psutil | |
import os | |
from datetime import datetime | |
def precise_repeat(f, interval=1.0, run=False): | |
delay = interval - (time.time() % interval) | |
threading.Timer(delay, precise_repeat, (f, interval, True)).start() | |
if run: | |
threading.Thread(target=f).start() | |
def worker(): | |
p = psutil.Process(os.getpid()) | |
print os.getpid(), p.get_num_threads(), time.time(), datetime.now() | |
if __name__ == "__main__": | |
precise_repeat(worker) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment