2013-10-20 30 views
0
#!/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() 

这个程序对目录中的搜索关键字, 但出现错误时:与queue.get错误,布尔对象没有属性得到

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' 

我怎样才能摆脱错误?

+0

't = keywordMatch(True)',你的意思是't = keywordMatch(queue)'? – zch

回答

1

你实例化线程与t = keywordMatch(True),然后在__init__你服用这种说法,并保存为self.queue - 所以很自然self.queue将是一个布尔值。如果你希望这是一个Queue实例有,你应该把它传递

+0

谢谢,我是多么愚蠢,哈哈哈,我知道了。 – user2876146

1

main()你写道:

t = keywordMatch(True) 

keywordMatch类的__init__做到这一点:

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

所以现在self.queueTrue!后来,试图做self.queue.get失败,因为它根本不是一个队列。

相关问题