Files
cursopython2023/aula196.py

51 lines
852 B
Python

# (Parte 2) Threads - Executando processamentos em paralelo
from threading import Thread
from time import sleep
"""
class MeuThread(Thread):
def __init__(self, texto, tempo):
self.texto = texto
self.tempo = tempo
super().__init__()
def run(self):
sleep(self.tempo)
print(self.texto)
t1 = MeuThread('Thread 1', 5)
t1.start()
t2 = MeuThread('Thread 2', 3)
t2.start()
t3 = MeuThread('Thread 3', 2)
t3.start()
for i in range(20):
print(i)
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)