global time import time global spMult spMult = 10 global spMinSleep spMinSleep = 0.1 global spMaxSleep spMaxSleep = 5 def f(env): with connectionsLock: connections[env["self"].cid]["spCmdStart"] = time.perf_counter() handlers["preCommand"].append(f) def f(env): passedTime = 0 with connectionsLock: if "spCmdStart" in connections[env["self"].cid]: passedTime = time.perf_counter() - connections[env["self"].cid]["spCmdStart"] if passedTime < 0: passedTime = 0 else: return sleepTime = passedTime*spMult if sleepTime > spMaxSleep: sleepTime = spMaxSleep if sleepTime < spMinSleep: sleepTime = spMinSleep print("sleeping for: " +str(sleepTime)) time.sleep(sleepTime) handlers["command"].append(f)