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


  • 相关阅读:
    测试用例设计方法
    软件测试--常考的证书
    fillder-----接口延时返回(新方法)
    测试工作之--adb代码
    设计测试用例的常用方法
    TFS2013 设置签出独占锁
    Sql Server (MSSQLSERVER) 服务无法启动
    Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
    C#使用redis学习笔记
    C#使用mybatis学习笔记
  • 原文地址:https://www.cnblogs.com/fonyer/p/9784868.html
Copyright © 2011-2022 走看看