Pythonで一定間隔で実行

Pythonで一定間隔で実行

Pythonで一定間隔であるコードを実行する方法を知べていたがなかなか見つからない。
標準ではないのかもしれない。微妙にこれでいいのか不安。

(今更気付いたがPythonの関数内では関数の外の変数も読めてしまうのですね)

import threading
import time
import sys

threadStop = False;
count = 0;
def run():
    global count, threadStop;
    count += 1;
    if threadStop != True:
        thread = threading.Timer(1, run)
        thread.start()
        print('run {0}'.format(count));
    else:
        threadStop = False;
    
    

thread = threading.Thread(target=run);
thread.start();

try:
    while (True):
        time.sleep(1);
    
except KeyboardInterrupt:
    threadStop = True;
    while threadStop == False:
        time.sleep(0.01);
    sys.exit();

(2018/08/25追記: .NETの支援があれば別の方法も使える)