(Parte 2) Threads - Executando processamentos em paralelo

This commit is contained in:
Luiz Otávio
2023-02-15 15:33:36 -03:00
parent 3af25680fb
commit 50810f471c

View File

@@ -1,10 +1,11 @@
# (Parte 1) Threads - Executando processamentos em paralelo # (Parte 2) Threads - Executando processamentos em paralelo
from threading import Thread from threading import Thread
from time import sleep from time import sleep
"""
class MeuThread(Thread): class MeuThread(Thread):
def __init__(self, texto: str, tempo: int): def __init__(self, texto, tempo):
self.texto = texto self.texto = texto
self.tempo = tempo self.tempo = tempo
@@ -27,3 +28,23 @@ t3.start()
for i in range(20): for i in range(20):
print(i) print(i)
sleep(1) sleep(1)
"""
def vai_demorar(texto: str, tempo: int):
sleep(tempo)
print(texto)
t1 = Thread(target=vai_demorar, args=('Olá mundo 1!', 5))
t1.start()
t2 = Thread(target=vai_demorar, args=('Olá mundo 2!', 1))
t2.start()
t3 = Thread(target=vai_demorar, args=('Olá mundo 3!', 2))
t3.start()
for i in range(20):
print(i)
sleep(.5)