Advertisement
Woobinda

Queue

Sep 16th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import threading
  2. import queue
  3.  
  4. class Worker(threading.Thread):
  5.  
  6.     def __init__(self, work_queue, word):
  7.         super(Worker,self).__init__()
  8.         self.work_queue = work_queue
  9.         self.word = word
  10.  
  11.     def run(self):
  12.         try:
  13.             filename = self.work_queue.get()
  14.             self.process(filename)
  15.         finally:
  16.             pass
  17.  
  18.     def process(self, filename):
  19.         previous = ''
  20.         current=True
  21.         with open(filename, "rb") as fh:
  22.             while current:
  23.                 current = fh.readline()
  24.                 if not current: break
  25.                 current = current.decode("utf8", "ignore")
  26.                 if self.word in current :
  27.                     print("find {0}: {1}".format(self.word,filename))
  28.                 previous = current
  29.  
  30. word = 'import'
  31. filelist = ['./file2.py','./file3.py','./file1.py']
  32. work_queue = queue.Queue()
  33. for filename in filelist:
  34.     work_queue.put(filename)
  35. for i in range(3):
  36.     worker = Worker(work_queue, word)
  37.     worker.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement