Skip to content
Snippets Groups Projects
Commit 2befe983 authored by shinedday's avatar shinedday
Browse files

Improved scheduler wait with semaphore

parent 9cd7e771
Branches
Tags
No related merge requests found
No preview for this file type
""" """
Scheduler class file Scheduler class file
""" """
from threading import Semaphore
from time import sleep from time import sleep
import sys import sys
...@@ -19,9 +20,6 @@ class Scheduler(Schedulable): ...@@ -19,9 +20,6 @@ class Scheduler(Schedulable):
def __init__(self, broker_ip: str) -> None: def __init__(self, broker_ip: str) -> None:
Schedulable.__init__(self, broker_ip, "Scheduler") Schedulable.__init__(self, broker_ip, "Scheduler")
self.sleep_between_cycle = 0
self.ihm_token = 0
self.paused = True self.paused = True
self.nbr_agent = 0 self.nbr_agent = 0
...@@ -32,8 +30,9 @@ class Scheduler(Schedulable): ...@@ -32,8 +30,9 @@ class Scheduler(Schedulable):
self.subscribe("ihm/pause", self.pause) self.subscribe("ihm/pause", self.pause)
self.subscribe("ihm/unpause", self.unpause) self.subscribe("ihm/unpause", self.unpause)
self.agent_waiting = 0 self.agent_semaphore = Semaphore(0)
self.schedulable_waiting = 0 self.schedulable_semaphore = Semaphore(0)
self.ihm_semaphore = Semaphore(0)
print("Init done") print("Init done")
...@@ -42,26 +41,26 @@ class Scheduler(Schedulable): ...@@ -42,26 +41,26 @@ class Scheduler(Schedulable):
Function called when the IHM pause the scheduler Function called when the IHM pause the scheduler
""" """
self.paused = True self.paused = True
self.ihm_token = 0 self.ihm_semaphore.acquire()
def unpause(self, client, userdata, message) -> None: def unpause(self, client, userdata, message) -> None:
""" """
Function called when the IHM unpause the scheduler Function called when the IHM unpause the scheduler
""" """
self.paused = False self.paused = False
self.ihm_token = 1 self.ihm_semaphore.release()
def step(self, client, userdata, message) -> None: def step(self, client, userdata, message) -> None:
""" """
Function called by the IHM when the scheduler is in Step by Step mode Function called by the IHM when the scheduler is in Step by Step mode
""" """
self.ihm_token += 1 self.ihm_semaphore.release()
def update_schedulable(self, client, userdata, message) -> None: def update_schedulable(self, client, userdata, message) -> None:
""" """
Function called whenever the amas/env have finished an action and is waiting Function called whenever the amas/env have finished an action and is waiting
""" """
self.schedulable_waiting += 1 self.schedulable_semaphore.release()
print("__Schedulable is waiting") print("__Schedulable is waiting")
def update_nbr_agent(self, client, userdata, message) -> None: def update_nbr_agent(self, client, userdata, message) -> None:
...@@ -77,24 +76,22 @@ class Scheduler(Schedulable): ...@@ -77,24 +76,22 @@ class Scheduler(Schedulable):
""" """
Called whenever an agent have done an action and is waiting Called whenever an agent have done an action and is waiting
""" """
self.agent_waiting += 1 self.agent_semaphore.release()
print("__Agent done") print("__Agent done")
def wait_agent(self) -> None: def wait_agent(self) -> None:
""" """
Called when the scheduler is waiting for all agent do have finished their action Called when the scheduler is waiting for all agent do have finished their action
""" """
while self.agent_waiting < self.nbr_agent: for _ in range(self.nbr_agent):
sleep(self.wait_delay) self.agent_semaphore.acquire()
self.agent_waiting = 0
def wait_schedulable(self) -> None: def wait_schedulable(self) -> None:
""" """
Called when the scheduler is waiting for both amas and env to have finished their action Called when the scheduler is waiting for both amas and env to have finished their action
""" """
while self.schedulable_waiting < 2: for _ in range(2):
sleep(self.wait_delay) self.schedulable_semaphore.acquire()
self.schedulable_waiting = 0
def wait_ihm(self) -> None: def wait_ihm(self) -> None:
""" """
...@@ -102,9 +99,7 @@ class Scheduler(Schedulable): ...@@ -102,9 +99,7 @@ class Scheduler(Schedulable):
""" """
if not self.paused: if not self.paused:
return return
while self.ihm_token == 0: self.ihm_semaphore.acquire()
sleep(self.wait_delay)
self.ihm_token -= 1
def first_part(self) -> None: def first_part(self) -> None:
""" """
...@@ -158,7 +153,6 @@ class Scheduler(Schedulable): ...@@ -158,7 +153,6 @@ class Scheduler(Schedulable):
print("-Last part") print("-Last part")
self.last_part() self.last_part()
sleep(self.sleep_between_cycle)
self.nbr_cycle += 1 self.nbr_cycle += 1
self.publish("scheduler/cycledone", "") self.publish("scheduler/cycledone", "")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment