Threading em Python
- Posted by acidx on September 27th, 2004 filed in geek
- Comment now »
Como não encontrei nenhum tutorial de Threading em Python, acabei fuçando em alguns programas e vi que não tem nenhum mistério (vai ver é por isso que não tem tutorial mesmo hehe). De qualquer maneira, aí vai um código mínimo que cria três threads e cada um imprime 10 vezes o que foi passado como argumento, com um intervalo de 0.2s para cada impressão.
import threading, time
class T(threading.Thread): def __init__(self, msg): threading.Thread.__init__(self) self.done = 0 self.msg = msg
def run(self): for i in range(0, 10): print self.msg time.sleep(0.2) self.done = 1
t1 = T("1")t2 = T("2")t3 = T("3")
t1.start()t2.start()t3.start()
Mais fácil que isso, impossível!
E sim, isso tem a ver com o meu programa de gravação de CDs.












Leave a Comment