Queue (Threads)

Thomas Guettler pan-newsreader at thomas-guettler.de
Fri Apr 12 15:10:58 EDT 2002


I programmed a Producer Consumer problem. But unfortunately
I get "cannot join thread before it is started".

I found a different approach which uses two Queues. 
http://www.bagley.org/~doug/shootout/bench/prodcons/alt/prodcons.python2.python

But I prefere a solution with just one queue:
 
import sys
from Queue import Queue
from threading import Thread

queue=Queue(4) #Only for items are allowed in the queue

def consume():
	print "Consuming makes fun"

class Producer(Thread):
	def run(self):
		print "Producing is hard"
		self.result="stone"

class Produce100Times(Thread):
	def run(self):
		for i in range(100):
			pt=Producer()
			queue.put(pt)
			pt.start()
		queue.put(None)

if (__name__=="__main__"):
	p100t=Produce100Times()
	p100t.start()
	while 1:
		produced_thread=queue.get()
		if produced_thread==None:
			#All items produced. Time to go home
			break
		print "Got Thread"
		produced_thread.join()
		print "Result:", produced_thread.result
		

I tried to find examples for the Queue class with google, but
no look. Has someone links to exmamples or hints?

 thomas

-- 
Thomas Guettler <guettli at thomas-guettler.de>
http://www.thomas-guettler.de



More information about the Python-list mailing list