You are using threads incorrectly in your code:
First, the code seems to be built on the wrong assumption that the one subclass object Threadyou have can spawn all the threads needed to do the job. On the contrary, the documentation says that "you need to call no more than once per object." In the case of a method, this is a link . ThreadstartThreadwordself
self.start(), , , . word Words, , Words, , Words . , word Words, :
def word():
queue = Queue.Queue()
read = open('words.txt')
for word in read:
word = word.replace("\n","")
self.put(word)
read.close()
, Words , :
class Words(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
-, (run) , . , , , , :
def run(self):
while True:
try:
word = self.queue.get(False)
except Queue.Empty:
break
print word
self.queue.task_done()
, , , , .
-, for self.run(), run, None , . , t None. , t = Word(queue), , t.start(). ,
class Words(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
try:
word = self.queue.get(False)
except Queue.Empty:
break
print word
self.queue.task_done()
def word():
queue = Queue.Queue()
read = open('words.txt')
for word in read:
word = word.replace("\n","")
self.put(word)
read.close()
for i in range(5):
t = Word()
t.setDaemon(True)
t.start()
queue.join()
if __name__=='__main__':
word()