from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed
from time import sleep, time
def festa_mais_tarde(tipo='', n=''):
sleep(3)
return f"{tipo}{n} hora da festa!"
def main():
with ProcessPoolExecutor() as executor_de_processos:
with ThreadPoolExecutor() as executor_de_threads:
tempo_inicial = time()
futuros = [
executor_de_processos.submit(festa_mais_tarde, tipo='proc', n='1'),
executor_de_processos.submit(festa_mais_tarde, tipo='proc', n='2'),
executor_de_threads.submit(festa_mais_tarde, tipo='thread', n='1'),
executor_de_threads.submit(festa_mais_tarde, tipo='thread', n='2')
]
for f in as_completed(futuros):
print(f.result())
tempo_final = time()
print(f"Tempo total para executar quatro funções de 3 segundos: {tempo_final - tempo_inicial}")
if __name__ == '__main__':
main()