import threading import os import time TIMEOUT = 30 class MoneyTransaction: def __init__(self, end_callback, add_money_callback) -> None: self.end_callback = end_callback self.add_money_callback = add_money_callback self.lock = threading.Lock() self.lock.acquire_lock(blocking=True) self.timer = None self.timer_lock = threading.Lock() def __end_callback(self, _ = None): print("End Callback") self.end_callback() with self.timer_lock: if self.timer is not None: self.timer.cancel() self.timer = None self.lock.release_lock() def start(self): print("Start Transaction") with self.timer_lock: self.timer = threading.Timer(TIMEOUT, self.__end_callback) # create Timeout self.timer.start() try: os.mkfifo("/tmp/fakegeld") except Exception as e: print(e) thread = threading.Thread(target = self.__loop) thread.start() def __loop(self): f = open("/tmp/fakegeld", "r") while True: time.sleep(0.1) line = f.readline() if line == "": continue print("read '%s'" % line) try: amount = float(line) print("got amount %s" % amount) self.add_money_callback(amount) except Exception as e: print(e) def stop(self): self.__end_callback()