Lower checking frequency on inactivity

This commit is contained in:
Fierelier 2021-04-09 19:11:52 +02:00
parent 4c12ffc375
commit 2f24db2f14

View File

@ -80,6 +80,10 @@ def addEventHandler(event,func):
def sendResponse(connection,data):
connection.sendall(len(data).to_bytes(4,"big") + data)
senderThreadSleepMin = 0.0333
senderThreadSleepMax = 1.0
senderThreadSleepIncr = 0.05
class senderThread(threading.Thread):
def __init__(self,connectionThread):
threading.Thread.__init__(self)
@ -87,24 +91,41 @@ class senderThread(threading.Thread):
with self.lock:
self.connectionThread = connectionThread
self.queue = []
self.newQueue = False
self.sleep = senderThreadSleepMin
def closeThread(self):
with self.lock:
self.queue = [["close"]]
self.newQueue = True
def addToQueue(self,entry):
with self.lock:
self.queue.append(entry)
self.newQueue = True
def run(self):
sleepTime = 0
while True:
time.sleep(0.0333)
with self.lock:
if len(self.queue) == 0: continue
sleepTime = self.sleep
print(sleepTime)
time.sleep(sleepTime)
with self.lock:
if not self.newQueue:
if self.sleep < senderThreadSleepMax:
self.sleep += senderThreadSleepIncr * self.sleep
if self.sleep > senderThreadSleepMax: self.sleep = senderThreadSleepMax
continue
for entry in self.queue:
if entry[0] == "close": return
entry[0](*entry[1],**entry[2])
self.queue = []
self.newQueue = False
self.sleep = senderThreadSleepMin
class connectionThread(threading.Thread):
def __init__(self,threadId,connection,address):