#encoding=GBK
#
# 这个是个多线程替换 文本
#
# 用队列进行线程之间的通讯
#
#
#
#
#
import os
import threading #引入线程模块
from Queue import Queue #队列 用于线程之间的通讯
########################################################################
class ReplaceFile(threading.Thread): #继承线程模块
"""替换多线程替换文本"""
#----------------------------------------------------------------------
def __init__(self,queue): #初始化线程
"""Constructor"""
threading.Thread.__init__(self)
self.files =queue
#----------------------------------------------------------------------
def run(self):
"""执行线程 执行的内容"""
#global mutex # 线程锁
#threadname=threading.currentThread().getName() #获取当前线程的名字 为线程运行提供方便
while True :
if self.files.empty():
break
M_file=self.files.get();
print M_file
#----------------------------------------------------------------------
########################################################################
class findFile(threading.Thread):
"""搜索文件添加到队列中"""
#----------------------------------------------------------------------
def __init__(self,queue,path):
"""Constructor"""
threading.Thread.__init__(self)
self.files =queue
self.path=path
#----------------------------------------------------------------------
def run(self):
""""""
for root, dirs, files in os.walk(self.path):
for mfile in files:
fpath=os.path.join(root, mfile)
#print fpath
if not os.path.isdir(fpath):
self.files.put(fpath)
#----------------------------------------------------------------------
if __name__ == "__main__":
root=raw_input("type root directory:")
key=raw_input("type key:")
"""执行主方法"""
print u'开始执行'
queue = Queue() #建立队列 进程之间的通讯
fF=findFile(queue,"D:\\vv")
fF.start()
threads = []
for i in xrange(5): #开5个线程 存入数组中
threads.append(ReplaceFile(queue))
for t in threads : # 开启所有线程
t.start()
for t in threads : # 等待关闭所有线程
t.join()
fF.join()