Skip to content

Instantly share code, notes, and snippets.

@drgarcia1986
Created May 6, 2014 22:04
Show Gist options
  • Save drgarcia1986/e55be3a1454429a4e7e1 to your computer and use it in GitHub Desktop.
Save drgarcia1986/e55be3a1454429a4e7e1 to your computer and use it in GitHub Desktop.
Exemplo simples de uso de Threads em python
#!/usr/bin/python
__author__ = 'Diego Garcia'
from threading import Thread
from time import sleep
from random import randint
class MinhaThread(Thread):
def __init__(self, identificador, vezes):
self.vezes = vezes
self.identificador = identificador
self.suspensa = False
Thread.__init__(self)
def parar(self):
self.suspensa = True
def run(self):
while (not self.suspensa) and self.vezes:
print("[{}] - execucoes restantes: {}".format(self.identificador, self.vezes))
self.vezes -= 1
sleep(randint(1, 10))
if __name__ == "__main__":
lista_threads = []
for i in range(1, 4):
t = MinhaThread(i, 10)
t.start()
lista_threads.append(t)
for t in lista_threads:
sleep(randint(10, 15))
print("Parando thread {}".format(t.identificador))
t.parar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment