一定時間間隔で実行するプログラムの書き方を学ぶために作ってみました。
clock.py
import time
import threading
import datetime
def timekeep(): # インターバル用関数
time.sleep(1) # 毎秒更新
def get_today(): # 時間取得
today = datetime.datetime.today()
value = (today.year, today.month, today.day,
today.hour, today.minute, today.second)
return value
def clock(): # 時計本体
test_tuple = get_today()
print("\r%s" % str(test_tuple), end='') # 出力を上書きする
funcs = [timekeep, clock] # 関数リスト
threads = []
current_time = time.time()
while(True): # 無限ループ。funcsに関して並行処理。timekeep関数終了時までclock関数は実行されない
for func in funcs:
t = threading.Thread(target=func)
t.start()
threads.append(t)
for t in threads:
t.join()
以上