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


posted on 2018-01-10 02:27  fonyer  阅读(235)  评论(0编辑  收藏  举报