Monitor_Valores.py

mail@pastecode.io avatar
unknown
python
2 years ago
1.2 kB
0
Indexable
Never
import threading
import numpy as np

class GestorValores():
    def __init__(self):
        self.__mutex = threading.Lock()
        self.__valores = []
        self.__finSistema = False
        self.__hasInserted = threading.Condition(self.__mutex)
        self.__isIntervalo: bool = False


    def InicioIntervalo(self):
        if self.__isIntervalo:
            self.__isIntervalo = True 


    def FinIntervalo(self):        
        if self.__isIntervalo:
            self.__isIntervalo = False
            self.__hasInserted.notify_all()
            return np.mean(self.__valores)
        return 0


    def InsertarValor(self, name, valor):       
        if self.__isIntervalo:
            with self.__mutex:
                # Bloquear hebra hasta que se acabe el intervalo.
                self.__hasInserted.wait_for(not self.__isIntervalo)
                # Añadir el valor
                self.__valor.append(valor)
        return


    def FinSistema(self):
        with self.__mutex:
            self.__finSistema = True 


    def EsFinSistema(self):
        with self.__mutex:
            return self.__finSistema