threading y Queue

Ramon Gonzalez rgmong en teleline.es
Jue Sep 13 19:19:05 CEST 2001


Hola,

Estoy intentando aprender sobre hilos y tengo dudas. Pongo el código
aquí para ver si alguien me puede decir en qué fallan las rutinas.
Al principio lo hice como aparece en 'threading1.pyw', pero despues vi
en el manual que existe una clase llamada Queue que está diseñada para
lo que pretendo hacer (según pude entender), e hice 'threading2.pyw'.

Saludos,
Ramon

===== threading1.pyw ==================================================
from threading import *

class hilillo(Thread):

    def __init__(self):
        Thread.__init__(self)

    def run(self):
        while len(lista)>0:
              nuevo = lista.pop()
              print nuevo

lista = [0,1,2,3,4,5,6,7,8,9,10]

h1 = hilillo()
h2 = hilillo()
h3 = hilillo()
h1.start()
h2.start()
h3.start()
h1.join()
h2.join()
h3.join()
===== fin threading1.pyw ============================================

 2º programa: (utilizando Queue)

===== threading2.pyw ================================================
from threading import *
from Queue import *

class hilillo(Thread):

    def __init__(self):
        Thread.__init__(self)

    def run(self):
        while not lista.empty():
              nuevo = lista.get()
              print nuevo

lista = Queue()
for x in range(11):
    lista.put(x)

h1 = hilillo()
h2 = hilillo()
h3 = hilillo()
h1.start()
h2.start()
h3.start()
h1.join()
h2.join()
h3.join()
===== fin threading2.pyw =============================================





Más información sobre la lista de distribución Python-es