初学多线程:
直接贴处下对比
(1)常规情况:
import threading
import os
import time
def check_files(path):
pathlist = os.listdir(path)
for mpath in pathlist:
mpath = path+'/'+mpath
if os.path.isdir(mpath):
check_files(mpath)
else:
reslist.append(mpath)
def main():
global reslist
reslist = []
path='D:\Program Files'
start =time.time()
check_files(path)
end =time.time()
print ('cost time :',end-start)
print ('files num is :',len(reslist))
if __name__ == '__main__':
main()
(2)多线程:
import threading
import os
import time
def check_files(path):
pathlist = os.listdir(path)
for mpath in pathlist:
mpath = path+'/'+mpath
if os.path.isdir(mpath):
check_files(mpath)
else:
reslist.append(mpath)
def main():
global reslist
reslist = []
path='D:\Program Files'
start =time.time()
threats =[]
t = threading.Thread(target=check_files,args=(path,))
threats.append(t)
for thread in threats:
thread.start()
for thread in threats:
thread.join()
end = time.time()
print ('cost time :',end-start)
print ('files num is :',len(reslist))
if __name__ == '__main__':
main()
执行结果没什么区别,因为该处仅仅是列出了文件名,但是如果有对文件的操作,区别有就有了:
如:增加了对图片文件是否包含二维码的判断:

在不使用多线程的情况下,总是1615个文件,204个目录的环境下,执行时间9.29秒

在使用多线程的情况下,总是1615个文件,204个目录的环境下,执行时间4.15秒

![[python] AES-GCM加解密函数-微慑信息网-VulSee.com](https://vulsee.com/wp-content/uploads/2024/12/4a3de68ff9eca0a01bae73a7470170f3.png)


![[python]文本处理-微慑信息网-VulSee.com](https://vulsee.com/wp-content/uploads/2023/12/e036ea90943d8195df3aa3e2ad3db122.png)
![[python] 输出颜色 - colorama / termcolor [补充]-微慑信息网-VulSee.com](https://vulsee.com/wp-content/uploads/2023/10/3ce37224d46fae3bee75b135ba129c77.png)
![[vulsee.com] 使用openpyxl 读取xlsx中的网址链接-微慑信息网-VulSee.com](https://vulsee.com/wp-content/uploads/2023/06/92d3798abeb4baabf03ebcf81d50e73e.png)







![[八卦] 王婷婷—揭秘一个大三女生的性爱录像-微慑信息网-VulSee.com](http://free.86hy.com/crack/pic/1.jpg)
![[随笔]今天国际警察节-微慑信息网-VulSee.com](http://photo.sohu.com/20041017/Img222528326.jpg)

青云网
