MiguelazoDS

Untitled

Oct 25th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. import Sensor_matrices
  2. import random
  3. import time
  4. import numpy
  5. import sys
  6. from multiprocessing import Process, Manager, Lock
  7. from multiprocessing.managers import BaseManager
  8.  
  9.  
  10. def alarmas(object, num_process, tiempo_limite):
  11.  
  12.     #bucle de 100 para el promedio
  13.     tiempo_inicio = time.time()
  14.  
  15.  
  16.     while (1):
  17. #        time.sleep(random.randint(1))
  18.         lista = object.get_obj()
  19. #        print("Vigilante " , lista)
  20.         for i in range (0,num_process):
  21.             if(lista[i] > 0):
  22.                 tiempo = time.time() - lista[i]
  23.                 object.set_value(i, -1)
  24.                 object.set_value_tiempos(tiempo)
  25.  
  26.  
  27. #    print (tiempos_atencion )
  28. #    media = numpy.mean(tiempos_atencion)
  29. #    print (tiempos_atencion )
  30. #    print (media )
  31. #    object.set_value_promedio(media)
  32. #    print ("termine" )
  33.  
  34.  
  35. class ListObj(object):
  36.         def __init__(self, lista, lista_tiempos_promedios):
  37.                 self.lista = lista
  38.                 self.lista_tiempos_promedios = lista_tiempos_promedios
  39.  
  40.         def set_value(self, indice_lista, tiempo_atencion):
  41.                 self.lista[indice_lista] = tiempo_atencion
  42.  
  43.         def set_value_tiempos(self, tiempo_atencion):  #para estadistica
  44.                 self.lista_tiempos_promedios.append(tiempo_atencion)
  45.  
  46.         def get_obj(self):
  47.                 return self.lista
  48.  
  49.         def get_value_tiempos(self): #para estadistica
  50.                 return self.lista_tiempos_promedios
  51.  
  52.  
  53. if __name__=="__main__":
  54.         num_process = 1
  55.         tamano_de_matriz = 10
  56.         if len(sys.argv) >2:
  57.             temp1=int(sys.argv[2])
  58.             if sys.argv[2].isdigit() and temp1 > 10 and temp1 < 100000:
  59.                 tamano_de_matriz=temp1#cantidad de valores
  60.         if len(sys.argv) >1:
  61.             temp2=int(sys.argv[1])
  62.             if sys.argv[1].isdigit() and temp2 > 1 and temp2 < 100:
  63.                 num_process = temp2 #cantidad de sensores
  64.  
  65.         BaseManager.register('ListObj', ListObj)
  66.         manager = BaseManager()
  67.         manager.start()
  68.  
  69.         tiempo_limite = 5 #tiempo que corre alarmas
  70.  
  71. #        while(num_process < 3 ):
  72.  
  73. #            num_bucles = num_bucles-1
  74.  
  75.         lista=list(range(num_process))
  76.         lista = [-1 for i in range(num_process)] #inicializamos con -1
  77.         lista_tiempos_promedios = []
  78.         listObl = manager.ListObj(lista,lista_tiempos_promedios)
  79.  
  80.         #print(listObl.get_obj())
  81.  
  82.         process_list = []
  83.         for p in range(num_process):
  84.                 proc = Sensor_matrices.Sensor(listObl, p, tamano_de_matriz)
  85.                 process_list.append(proc)
  86.  
  87.  
  88.         for x in range(num_process):
  89.                 process_list[x].start()
  90.  
  91.         alarmas= Process(target=alarmas, args=(listObl,num_process, tiempo_limite))
  92.         alarmas.start()
  93.  
  94.         time.sleep(tiempo_limite)
  95.  
  96.         for x in range(num_process):
  97.                 process_list[x].terminate()
  98.  
  99.         alarmas.terminate()
  100.  
  101. #            print (listObl.get_value_tiempos() )
  102.         media = numpy.mean(listObl.get_value_tiempos() ) #promedio de ti
  103.         print(media)
  104.  
  105.     #        for x in range(num_process):
  106.     #                process_list[x].join()
Add Comment
Please, Sign In to add comment