zoukankan      html  css  js  c++  java
  • 多进程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('
    复制的进度是:%.2f%%'%rate,end="")
     
        #关闭进程池,不再接受新的进程
        #pool.close()
        #主进程阻塞等待子进程的退出
        #pool.join()
    if __name__ == '__main__':
        t_start = time.time()
        main()     
        t_stop = time.time()
        print("
    执行完毕,耗时%0.2f"%(t_stop-t_start))

    单进程:

    C:UsersAdministrator>python d:pythoncopy.py

    复制的进度是:100.00%

    执行完毕,耗时6.53


    多进程:

    C:UsersAdministrator>python d:pythoncopy.py

    复制的进度是:100.00%

    执行完毕,耗时6.59


  • 相关阅读:
    SpringDataJpa项目中的使用
    js 中时间格式化的几种方法
    程序员业余赚钱
    20年研发管理经验谈(十七)
    SpringBoot第二十二篇:应用监控之Actuator
    20年研发管理经验谈(十五)
    SpringBoot第二十一篇:整合ActiveMQ
    RPC理解
    20年研发管理经验谈(十四)
    SpringBoot第二十篇:初识ActiveMQ
  • 原文地址:https://www.cnblogs.com/fonyer/p/9784868.html
Copyright © 2011-2022 走看看