问题描述
- Python多线程执行失败
-
我使用python编程实现查找某根目录下所有重复文件的功能,新手小白。
但是通过单步调试后,发现thread2.start()好像完全没有执行,直接就跳出循环了。
请高手看看~
解决方案
附代码
-*- coding: utf-8 -*-
import threading
import os
import os.path
import sys
import hashlib
def findFile1(rootPath, fileSeq, delSeq):
dirs = os.listdir(rootPath) #list the directories under the root path
for dir in dirs[0:len(dirs) // 2]: #traversal all the directories
path = rootPath + os.sep + dir #complete the path of current file
if os.path.isdir(path):
findFile1(path, fileSeq, delSeq) #if current file is a directory, recursive the function
else:
md5Check(path, fileSeq, delSeq) #if not a directory, check the md5
def findFile2(rootPath, fileSeq, delSeq):
# type: (object, object, object) -> object
dirs = os.listdir(rootPath) #list the directories under the root path
for dir in dirs[len(dirs) // 2 + 1:len(dirs)]: #traversal all the directories
path = rootPath + os.sep + dir #complete the path of current file
if os.path.isdir(path):
findFile2(path, fileSeq, delSeq) #if current file is a directory, recursive the function
else:
md5Check(path, fileSeq, delSeq)
def md5Check(path, fileSeq, delSeq):
f = file(path, 'rb') #open the file with 'read-only' and 'binary'
md5obj = hashlib.md5()
md5obj.update(f.read()) #calculate the md5
if md5obj.hexdigest() in fileSeq:
mutex1.acquire()
delSeq.append(path)
mutex1.release() #if md5 of current file is in the fileSeq, put the file path into the delSeq
else:
mutex2.acquire()
fileSeq.append(md5obj.hexdigest()) #if not in the fileSeq, put the md5 into the fileSeq
mutex2.release()
f.close() #close the file
def delList(delSeq):
print 'These files are waiting to be removed:'
for delFile in delSeq:
print delFile #list the file path in the delSeq
class myThread1 (threading.Thread):
def init(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print "Starting " + self.name
# 获得锁,成功获得锁定后返回True
# 可选的timeout参数不填时将一直阻塞直到获得锁定
# 否则超时后将返回False
findFile1(rootPath, fileSeq, delSeq)
# 释放锁
class myThread2 (threading.Thread):
def init(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print "Starting " + self.name
findFile2(rootPath, fileSeq, delSeq)
# 释放锁
mutex1 = threading.Lock()
mutex2 = threading.Lock()
threads = []
创建新线程
thread1 = myThread1(1, "Thread-1")
thread2 = myThread2(2, "Thread-2")
global fileSeq
fileSeq = []
global delSeq
delSeq = []
while True:
if len(sys.argv) == 1:
global rootPath
rootPath = raw_input('Enter the root path: ') #one parameter means no parameter, ask the root path
else:
rootPath = sys.argv[1] #or get the second parameter as the root path
try:
# 开启新线程
thread1.start()
thread2.start()
#try if the root path is valid
except(OSError):
print 'The root path is invalid. Please enter again. '
del sys.argv[1:]
continue #catch the except and delete all invalid parameters
break
if len(delSeq) == 0 :
print 'No duplicate file was found! ' #if no files in delSeq, exit
else:
delList(delSeq) #or list the delSeq
while True:
answer = raw_input('Would you want to remove these files? Please answer yes(y) or no(n): ')
answer.lower
if answer in ('yes', 'y'): #if "yes"
for delFile in delSeq:
try:
os.remove(delFile) #remove all files in delSeq
except(OSError):
print 'Warning! "%s" is not existed! ' % delFile
continue #ignore the files witch are not existed
print 'All duplicate files have been removed! '
break
elif answer in ('no', 'n'):
print 'Process has exited without any change! '
break #if "no", do nothing
else:
print 'Please enter yes(y) or no(n). '
sys.exit()
添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
等待所有线程完成
for t in threads:
t.join()
print "Exiting Main Thread"