multiprocessing_Manager().Queue()_Pool

This code is to realize copy multiple files in one file to the other file by Pool in multiprocessing ,to reduce the time.

import time
from multiprocessing import Pool,Manager
import os
def copyFileTask(name,oldFile,newFile,queue):
    queue.put(name)
    fr = open(oldFile+'/'+name)
    fw = open(newFile+'/'+name,'w')

    content = fr.read()
    fw.write(content)

    fr.close()
    fw.close()

def main():
    oldFolder = input('Please input the name of folder')
    newFolder = oldFolder + '-copy'

    os.mkdir(newFolder)

    fileName = os.listdir(oldFolder)
    queue = Manager().Queue()
    pool = Pool(5)
    for f in fileName:
        pool.apply_async(copyFileTask,args=(f,oldFolder,newFolder,queue,))
    pool.close()
    pool.join()
    allnum = len(fileName)
    print(allnum)
    num=0
    while queue.empty()==False:
        num=num+1
        queue.get()
        time.sleep(1)
        copyrate = num/allnum
        print('\rThe process %% %.2f'%(copyrate*100),end="")
    print('\nThe copy has been finished')
if __name__=='__main__':
    main()


你可能感兴趣的:(python)