from time import sleep from random import binomialvariate DISPLAY = "display" SENSOR = "sensor" ZIGBEE = "zigbee" LABELS = {DISPLAY, SENSOR, ZIGBEE} time = 0 transactions = [] active_transaction = None slice_labels = {l: "" for l in LABELS} def display(): if time % 5 == 1: transactions.append((DISPLAY, 4 if binomialvariate(p=0.15) else 2, 50)) def sensor(): if time % 5 == 0: transactions.append((SENSOR, 1, 10)) def zigbee(): if time % 15 == 14: transactions.append((ZIGBEE, 10, 100)) print(' '*5, *(l[0] for l in LABELS)) while True: display() sensor() zigbee() if active_transaction is None and transactions: active_transaction = min(transactions, key=lambda t: t[2]) transactions.remove(active_transaction) print(f"t={time:02}: ", end="") if active_transaction is not None: label, duration, priority = active_transaction print(*('x' if l == label else '#' if any(t[0]==l for t in transactions) else ' ' for l in LABELS)) active_transaction = (label, duration-1, priority) if duration > 1 else None transactions = [(l, d, p*0.8) for (l, d, p) in transactions] else: print(" ") time += 1 sleep(1)