Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
#!/usr/bin/env python
#coding=utf-8
import sys,os,threading
import Queue


keyword = sys.argv[1]
path = sys.argv[2]


class keywordMatch(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        while True:
            line = self.queue.get()
            if keyword in line:
                print line

            queue.task_done()
def main():
    concurrent = 100 # Number of threads
    queue = Queue.Queue()

    for i in range(concurrent):
        t = keywordMatch(True)
        t.setDaemon(True)
        t.start()

    allfiles = os.listdir(path)
    for files in allfiles:
        pathfile = os.path.join(path,files)
        fp = open(pathfile)
        lines = fp.readlines()
        for line in lines:
            queue.put(line.strip())
    queue.join()


if __name__ == '__main__':
    main()

This program is for searching the keyword in a directory, but there occurs an error:

Exception in thread Thread-100:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "du.py", line 17, in run
    line = self.queue.get()
AttributeError: 'bool' object has no attribute 'get'

How can I get rid of the error?

share|improve this question
    
t = keywordMatch(True), did you mean t = keywordMatch(queue)? –  zch Oct 20 '13 at 12:18

2 Answers 2

up vote 1 down vote accepted

You're instantiating the thread with t = keywordMatch(True), and then in __init__ you're taking this argument and saving it as self.queue - so naturally self.queue is going to be a bool. If you want there to be a Queue instance there, you should pass it in.

share|improve this answer
    
Thanks, How stupid i was, hahaha.now i've got it. –  user2876146 Oct 20 '13 at 12:28

In main() you wrote:

t = keywordMatch(True)

The keywordMatch class's __init__ does this:

def __init__(self,queue):
    self.queue = queue

So now self.queue is True! Later, trying to do self.queue.get fails because it isn't a queue at all.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.