多進程copy文件
阿新 • • 發佈:2018-01-10
apply odi pool 耗時 man 消息隊列 con listdir copy from multiprocessing import Pool,Manager
import os,time
def copyFileTask(fileName,oldFolderName,newFolderName,queue):
fr = open(oldFolderName+"/"+fileName,'r',encoding='UTF-8')
fw = open(newFolderName+"/"+fileName,'w',encoding='UTF-8')
#復制
while True:
content = fr.read(1024)
if len(content) == 0:
break
fw.write(content)
#關閉文件
fr.close()
fw.close()
queue.put(fileName)
def main():
oldFolderName = 'test'#input("請輸入要復制的文件夾名字:")
#創建目錄
newFolderName = oldFolderName+"[復件]"
os.mkdir(newFolderName)
#獲取文件列表
fileList = os.listdir(oldFolderName)
#使用多進程的方式復制
pool = Pool(5)
#消息隊列
queue = Manager().Queue()
for file in fileList:
#copyFileTask(file,oldFolderName,newFolderName,queue)
pool.apply_async(copyFileTask,args=(file,oldFolderName,newFolderName,queue))
num = 0
total = len(fileList)
while num<total:
queue.get()
num += 1
rate = num/total*100
print('\r復制的進度是:%.2f%%'%rate,end="")
#關閉進程池,不再接受新的進程
#pool.close()
#主進程阻塞等待子進程的退出
#pool.join()
if __name__ == '__main__':
t_start = time.time()
main()
t_stop = time.time()
print("\n執行完畢,耗時%0.2f"%(t_stop-t_start))
單進程:
C:\Users\Administrator>python d:\python\copy.py
復制的進度是:100.00%
執行完畢,耗時6.53
多進程:
C:\Users\Administrator>python d:\python\copy.py
復制的進度是:100.00%
執行完畢,耗時6.59
多進程copy文件