Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import multiprocessing, random
  2. class maquina(multiprocessing.Process):
  3.     def __init__(self,id,saida):
  4.         self.id = id
  5.         self.saida = saida
  6.         multiprocessing.Process.__init__(self)
  7.     def run(self):
  8.         while self.id.empty() == False:
  9.             re = []
  10.             resultado = self.id.get()
  11.             re.append(resultado[1])
  12.             if resultado[0] != 5:
  13.                 re.append('Sucess')
  14.             else:
  15.                 re.append('Loss')
  16.             print(re)
  17.             self.saida.put(re)
  18.             self.id.task_done()
  19. class produto(object):
  20.     def __init__(self):
  21.         self.sucesso = random.randint(1,5)
  22.         self.tempo = random.randint(2,7)
  23.     def vai(self):
  24.         return (self.sucesso,self.tempo)
  25.  
  26. if __name__ == '__main__':
  27.     tempo = 0
  28.     pfeitos = 0
  29.     total = 0
  30.     produtos = 60
  31.     horas = 10
  32.     fila = multiprocessing.JoinableQueue()
  33.     saida = multiprocessing.Queue()
  34.     ms = []
  35.     for c in range(horas):
  36.         for c in range(produtos):
  37.             fila.put(produto().vai())
  38.         for c in range(3):
  39.             ms.append(maquina(fila,saida))
  40.         for c in ms:
  41.             c.start()
  42.  
  43.         for c in ms:
  44.             c.join()
  45.         input('next')
  46.     while saida.empty() != True and tempo < 60*horas:
  47.         pfinal = saida.get()
  48.         tempo += pfinal[0]
  49.         total+= 1
  50.         if pfinal[1] == 'Sucesso': pfeitos += 1
  51.     print(f'Total of pieces made:{total}\nProduct losses:{total-pfeitos} Time:{tempo}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement