python/thread.py

from datetime import datetime
import time
import threading
s0=datetime.utcnow()
print(f"start {__file__} now={s0}")
s1=datetime.utcnow()
print(f"start {__file__} now={s1} diff {s1-s0} {(s1-s0).total_seconds()}")
time.sleep(0.11)
s1=datetime.utcnow()
print(f"after sleep 0.11 now={s1} diff {s1-s0} {(s1-s0).total_seconds()}")

class TiLo(threading.Thread):
    stop = False;
    def __init__(self, n, i=2.2, m=10):
        threading.Thread.__init__(self)
        self.name = n
        self.iv = i
        self.max = m

    def run(self):
        t0=datetime.utcnow()
        print(f"TiLo{self.name} run begin {t0}")
        d1 = 0
        while d1 < self.max and not TiLo.stop:
            time.sleep(self.iv)
            t1=datetime.utcnow()
            d1 = (t1 - t0).total_seconds()
            print(f"TiLo{self.name} run interval {t1} diff {d1} last={d1 >= self.max}")

aTh =TiLo('aEins', 2.3)
aTh.start()
TiLo('sZwei', 1.1, 6).run()

print(f"after TiLo zwei threads {threading.enumerate()}")
TiLo.stop = True;
aTh.join()
print(f"after join aEins threads {threading.enumerate()}")