made Pipeline Threadsave

This commit is contained in:
schrom01 2022-08-23 14:31:23 +02:00
parent 633750d0dc
commit 6fda077f9b
2 changed files with 25 additions and 18 deletions

View File

@ -12,8 +12,8 @@ class ZoneManager:
self.zones = fileIO.loadZones()
for zone in self.zones:
zone.setZoneManager(self)
self.pipeLine = [] #todo Thread Synchron
#self.lock = threading.Lock()
self.pipeLine = []
self.piplineMutexLock = threading.Lock()
def getZone(self, number):
for zone in self.zones:
@ -21,22 +21,23 @@ class ZoneManager:
return zone
def addIrrigationJob(self, job):
#with self.lock:
with self.piplineMutexLock:
self.pipeLine.append(job)
def deleteIrrigationJobByID(self, id):
#with self.lock:
i = 0
with self.piplineMutexLock:
while i < (len(self.pipeLine)):
irrigationJob = self.pipeLine[i]
if(irrigationJob.id == id):
self.pipeLine.pop(i)
break
else:
i = i + 1
def deleteIrrigationJobsForZone(self, zone):
#with self.lock:
i = 0
with self.piplineMutexLock:
while i < (len(self.pipeLine)):
irrigationJob = self.pipeLine[i]
if (irrigationJob.zone == zone):
@ -62,7 +63,7 @@ class ZoneManager:
def getPlanedDurationForZone(self, zone):
totalDuration = 0
#with self.lock:
with self.piplineMutexLock:
for irrigationJob in self.pipeLine:
if(irrigationJob.zone == zone):
totalDuration = totalDuration + irrigationJob.duration
@ -73,16 +74,21 @@ class ZoneManager:
zone.refreshState()
def processPipeline(self):
#with self.lock:
self.piplineMutexLock.acquire()
if(len(self.pipeLine) > 0 and (not self.isAnyZoneBusy())):
irrigationJob = self.pipeLine[0]
irrigationJob.process()
self.pipeLine.pop(0)
self.piplineMutexLock.release()
irrigationJob.process()
else:
self.piplineMutexLock.release()
def cronJobs(self):
self.refreshStates()
self.processPipeline()
print("Executed Cron Jobs of ZoneManager.\nactual Time: " + str(time()))
print("Executed Cron Jobs of ZoneManager.")

View File

@ -13,8 +13,9 @@ webserver = Webserver(zoneManager=zoneManager, port=80)
def cronJobs():
while True:
time.sleep(0.5)
zoneManager.cronJobs()
print("Cronjobs done\nactual Time: " + str(time.time()))
time.sleep(0.5)
if __name__ == "__main__":
cronjob_Thread = threading.Thread(target=cronJobs)