我使用python编程实现查找某根目录下所有重复文件的功能,新手小白。
但是通过单步调试后,发现thread2.start()好像完全没有执行,直接就跳出循环了。
请高手看看~
Python多线程执行失败
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- qq_31169503 2016-05-25 13:43关注
附代码
-*- coding: utf-8 -*-
import threading
import os
import os.path
import sys
import hashlibdef 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 md5def 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 filedef delList(delSeq):
print 'These files are waiting to be removed:'
for delFile in delSeq:
print delFile #list the file path in the delSeqclass 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
breakif 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"解决 无用评论 打赏 举报
悬赏问题
- ¥15 为什么eprime输出的数据会有缺失?
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
- ¥15 让node服务器有自动加载文件的功能
- ¥15 jmeter脚本回放有的是对的有的是错的
- ¥15 r语言蛋白组学相关问题