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()


發佈了174 篇原創文章 · 獲贊 64 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章