Created
November 30, 2021 11:38
-
-
Save thaisviana/3dee5ac5ce21dec876be49cc271dbe6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Process, Queue | |
import time | |
from random import randint | |
def calcula_percent(q_entrada,q_saida): | |
# Realiza o cálculo | |
lista_fatiada = q_entrada.get() | |
resultado = [] | |
for elemento in lista_fatiada: | |
resultado.append(elemento * 0.1) | |
q_saida.put() | |
if __name__ == "__main__": | |
N = 1000000 | |
# Captura tempo inicial | |
t_inicio = float(time.time()) | |
# Apenas preenchendo a lista de forma simplificada: | |
lista = [] | |
for i in range(N): | |
lista.append(randint(1,1000)) | |
# Fila de entrada dos processos | |
q_entrada = Queue() | |
# Fila de saída dos processos | |
q_saida = Queue() | |
lista_de_processos = [] | |
q_entrada.put(lista[0:N//2]) | |
p0 = Process(target=calcula_percent, args=(q_entrada,q_saida)) | |
p0.start() | |
lista_de_processos.append(p0) | |
q_entrada.put(lista[N//2:N]) | |
p1 = Process(target=calcula_percent, args=(q_entrada,q_saida)) | |
p1.start() | |
lista_de_processos.append(p1) | |
lista_nova = [] | |
for i in range(2): | |
lista_nova.extend(q_saida.get()) | |
# Captura tempo final | |
t_fim = float(time.time()) | |
print("Tempo total c/ 2 processos em segundos:", t_fim - t_inicio) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment