Thread scripts stop after completion without closing

Hope this is just something small, I'm wrong, as these are some of my first threads scripts using queues. Basically, after a run, he stops and sits there, but does not exit.

import threading
import Queue
class Words(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.queue = Queue.Queue()     

    def word(self):
        read = open('words.txt')
        for word in read:
            word = word.replace("\n","")
            self.queue.put(word)       
        read.close() 
        for i in range(5):
            t = self.run()
            t.setDaemon(True)
            t.start()  
        self.queue.join()

    def run(self): 
        while True:
            word = self.queue.get()
            print word 
            self.queue.task_done()

    if __name__ == '__main__':
        Word =  Words()
        Word.word()
+3
source share
3 answers

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()
+2

, , . , for i in range(5): , , .

:

import threading
import Queue

class Worker(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        while True:
            # try to dequeue a word from the queue
            try:
                word = self.queue.get_nowait()
            # if there nothing in the queue, break because we're done
            except Queue.Empty:
                break

            # if the 'try' was successful at getting a word, print it
            print word


def fill_queue(queue):
    read = open('words.txt')
    for word in read:
        word = word.replace("\n", "")
        queue.put(word)
    read.close()


if __name__ == "__main__":
    # create empty queue
    queue = Queue.Queue()

    # fill the queue with work
    fill_queue(queue)

    # create 5 worker threads
    threads = []
    for i in range(5):
        threads.append(Worker(queue))

    # start threads
    for thread in threads:
        thread.start()

    # join threads once they finish
    for thread in threads:
        thread.join()
0

All Articles