def run(self):
        print("Entrou no coordenador ", self.topic)
        cont = Cons(".\python.config")
        cont.subscribe([self.topic])
        bodies = []
        while True:
            try:
                data, err = self.timeout(cont, 15)
                print(f'Recebi data: {data}')
                if data == None:
                    continue
                elif err != None:
                    continue
                else:
                    bodies.append(data)
            except TypeError:
                # caso dê exesão, conferir se tem uam unica palavra a ser somada
                print("timeout")
                for topic in self.coordinatorsTopic:
                    print(f'publicando em coordenador')
                    for body in bodies:
                        prod = Prod("./python.config", topic)
                        prod.publish("data", body)
                        print(f'Publicando em {topic} - {str(body)}')
                        try:
                            prod.flush()
                        except:
                            pass
                if bodies == []:
                    continue
                self.run2(cont, bodies.copy())
                bodies = []

        cont.close()
Exemple #2
0
 def publishToReduce(self, data, file, word):
     data = self.clear(data)
     prod = Prod("./python.config", "reduce")
     prod.publish("data", {'data': data, 'file': file, 'word': word})
     try:
         prod.flush()
     except:
         pass
Exemple #3
0
    def run(self):
        print("Entrou no run")
        cont = Cons(".\python.config")
        cont.subscribe([self.topic])
        while True:
            reicivedBodies = []
            for body in self.bodies:
                prod = Prod("./python.config", self.coordenationTopic)
                prod.publish("data", body.__dict__)
                print(
                    f'Publicando em {self.coordenationTopic} - {body.__dict__}'
                )
                try:
                    prod.flush()
                except:
                    pass
            while True:
                try:
                    data, err = self.timeout(cont, 50)
                    print(f'Recebi data: {data}')
                    if data == None:
                        continue
                    elif err != None:
                        continue
                    else:
                        reicivedBodies.append(
                            Body(int(data['x']), int(data['y']),
                                 int(data['size']), int(data['xDisplacement']),
                                 int(data['yDisplacement'])))
                        print("Received bodies: ")
                except TypeError:
                    print("Entrou no exception")
                    if reicivedBodies == []:
                        print("reicivedBodies == []")
                        continue
                    with concurrent.futures.ThreadPoolExecutor(4) as executor:
                        # dividir para as threads
                        results = executor.map(self.changeAll,
                                               [(i, reicivedBodies.copy())
                                                for i in self.bodies])
                        resultados = []
                        print("Trabalhando sobre: ", reicivedBodies)
                        print("Resultados")
                        for result in results:
                            resultados.append(result)
                            print("  ->", result.__dict__)
                        print("Atulização")
                        for i, result in enumerate(resultados):
                            self.bodies[i] = result
                    break
            # break

        cont.close()
Exemple #4
0
    def work(self, data, nMachine, word):
        print("Entrou no worker 1, ")
        new_data = [int(num) for num in data.split(' ') if num != '']
        print("Entrou no worker 1, ", new_data)
        try:
            new_data = splited(new_data, int(nMachine))[self.id]
            print("Dado splitado: ", new_data)
        except IndexError:
            print("nao é o seu id")
            return


        data = str(OperationParalel(sum, operation2, os.cpu_count()//2).run(new_data))
        print("Publicando: ", data)
        prod = Prod("./python.config", "cont")
        prod.publish("data", {'data': data, 'word': word})
        try:
            prod.flush()
        except:
            pass
Exemple #5
0
    def reduce(self, data, file, word):
        new_data = data.split(' ')
        file_name = file
        search_word = word

        def operation1(words):
            nonlocal search_word
            search = search_word
            cont = 0
            for word in words:
                if word == search:
                    cont += 1
            return cont

        data = str(
            OperationParalel(operation1, operation2,
                             os.cpu_count() // 2).run(new_data))

        prod = Prod("./python.config", "cont")
        prod.publish("data", {'data': data, 'word': word})
        try:
            prod.flush()
        except:
            pass
 def run2(self, cont, bodies):
     while True:
         try:
             data, err = self.timeout(cont, 15)
             print(f'Recebi data: {data}')
             if data == None:
                 continue
             elif err != None:
                 continue
             else:
                 bodies.append(data)
         except TypeError:
             print("timeout run2 ", self.topic)
             for topic in self.topicClients:
                 for body in bodies:
                     prod = Prod("./python.config", topic)
                     prod.publish("data", body)
                     print(f'Publicando runc2 em {topic} - {str(body)}')
                     try:
                         prod.flush()
                     except:
                         pass
             bodies = []
             break
Exemple #7
0
    def run(self):
        cont = Cons(".\python.config")
        cont.subscribe(["cont"])
        dataFinal = {}
        while True:
            try:
                data, err = timeout(cont)
                print(f'Recebi data: {data}')
                if data == None:
                    continue
                elif err != None:
                    continue
                else:
                    word = data['word']
                    data = data['data']

                    if dataFinal.get(word) is None:
                        dataFinal[word] = data + ' '
                    else:
                        dataFinal[word] += data + ' '

                    if self.dictWord.get(word) is None:
                        self.dictWord[word] = 0

                    print(f'Datafinal: {dataFinal}')
                    print(f'Contagem atual: {self.dictWord}')
            except Exception:
                # caso dê exesão, conferir se tem uam unica palavra a ser somada
                print("timeout")
                for key, value in dataFinal.items():
                    splitado = [
                        int(num) for num in dataFinal[key].split(' ')
                        if num != ''
                    ]
                    if 0 < len(splitado) < 3:
                        print(f'tamanho < 3, valor atul: {self.dictWord[key]}')
                        print(f'Valor a somar: {sum(splitado)}')
                        self.dictWord[key] += sum(splitado)
                        dataFinal[key] = ''
                        prod = Prod("./python.config", key)
                        prod.publish("data", {
                            'data': str(self.dictWord[key]),
                            'word': word
                        })
                        print(
                            f'Publicando em {key} - {str(self.dictWord[key])}')
                        try:
                            prod.flush()
                        except:
                            pass
                    elif len(splitado) > 2:
                        for topic in self.topics:
                            prod = Prod("./python.config", topic)
                            prod.publish(
                                "data", {
                                    'data': value,
                                    'word': word,
                                    'machine': self.nMachines
                                })
                            print(f'Publicando em machines - {value}')
                            try:
                                prod.flush()
                            except:
                                pass
                        dataFinal[key] = ''

        cont.close()