Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: cp1252 -*-
- import threading
- import time
- class hilo1(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- self.valor=2.
- def run(self):
- print "Soy el hilo que calcula.",
- print "No volverás a saber de mí."
- print
- while True:
- self.valor=self.valor+1/self.valor # Calculamos algo
- time.sleep(0.2) # y a dormir para no subir demasiado rápido.
- if self.valor>10: break # Me canso y termino.
- class contador(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- def run(self):
- print "Soy el hilo contador."
- while True:
- time.sleep(1)
- print "Ha pasado otro segundo. ",
- print h.valor # Accede al otro hilo y averigua ese valor.
- if h.valor>10: break # Fin.
- h=hilo1()
- h.start() # Lanza el cálculo.
- time.sleep(0.1) # Para evitar que los prints se mezclen.
- t=contador()
- t.start() # Lanza el contador.
- h.join() # Espera a que termine de ejecutarse h.
- print 'Fin'
Advertisement
Add Comment
Please, Sign In to add comment