LoginSignup
1
0

More than 3 years have passed since last update.

勉強用にコンソール時計を作った

Last updated at Posted at 2019-08-02

一定時間間隔で実行するプログラムの書き方を学ぶために作ってみました。

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()

以上

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0