(Parte 3) Threads - Executando processamentos em paralelo
This commit is contained in:
75
aula196.py
75
aula196.py
@@ -1,6 +1,6 @@
|
|||||||
# (Parte 2) Threads - Executando processamentos em paralelo
|
# (Parte 3) Threads - Executando processamentos em paralelo
|
||||||
|
|
||||||
from threading import Thread
|
from threading import Lock, Thread
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@@ -30,8 +30,8 @@ for i in range(20):
|
|||||||
sleep(1)
|
sleep(1)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
def vai_demorar(texto: str, tempo: int):
|
def vai_demorar(texto, tempo):
|
||||||
sleep(tempo)
|
sleep(tempo)
|
||||||
print(texto)
|
print(texto)
|
||||||
|
|
||||||
@@ -48,3 +48,70 @@ t3.start()
|
|||||||
for i in range(20):
|
for i in range(20):
|
||||||
print(i)
|
print(i)
|
||||||
sleep(.5)
|
sleep(.5)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
def vai_demorar(texto, tempo):
|
||||||
|
sleep(tempo)
|
||||||
|
print(texto)
|
||||||
|
|
||||||
|
|
||||||
|
t1 = Thread(target=vai_demorar, args=('Olá mundo 1!', 10))
|
||||||
|
t1.start()
|
||||||
|
t1.join()
|
||||||
|
|
||||||
|
print('Thread acabou!')
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class Ingressos:
|
||||||
|
"""
|
||||||
|
Classe que vende ingressos
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, estoque: int):
|
||||||
|
""" Inicializando...
|
||||||
|
|
||||||
|
:param estoque: quantidade de ingressos em estoque
|
||||||
|
"""
|
||||||
|
self.estoque = estoque
|
||||||
|
# Nosso cadeado
|
||||||
|
self.lock = Lock()
|
||||||
|
|
||||||
|
def comprar(self, quantidade: int):
|
||||||
|
"""
|
||||||
|
Compra determinada quantidade de ingressos
|
||||||
|
|
||||||
|
:param quantidade: A quantidade de ingressos que deseja comprar
|
||||||
|
:type quantidade: int
|
||||||
|
:return: Nada
|
||||||
|
:rtype: None
|
||||||
|
"""
|
||||||
|
# Tranca o método
|
||||||
|
self.lock.acquire()
|
||||||
|
|
||||||
|
if self.estoque < quantidade:
|
||||||
|
print('Não temos ingressos suficientes.')
|
||||||
|
# Libera o método
|
||||||
|
self.lock.release()
|
||||||
|
return
|
||||||
|
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
self.estoque -= quantidade
|
||||||
|
print(f'Você comprou {quantidade} ingresso(s). '
|
||||||
|
f'Ainda temos {self.estoque} em estoque.')
|
||||||
|
|
||||||
|
# Libera o método
|
||||||
|
self.lock.release()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ingressos = Ingressos(10)
|
||||||
|
|
||||||
|
for i in range(1, 20):
|
||||||
|
t = Thread(target=ingressos.comprar, args=(i,))
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
print(ingressos.estoque)
|
||||||
|
|||||||
Reference in New Issue
Block a user