python 多进程读取五千万个文件_Python多进程练习(通过多进程、进程间通信等实现60个G的电影拷贝)...

此篇为学习Python多进程这个知识点是的练习Demo,有兴趣的可以看一下。这 里面涉及到的知识点有:进程池(Pool)、进程间通信(Manager().Queue() > 、文件读写等。

# coding:utf-8

import os

from multiprocessing import Pool, Manager

# 定义一个copy文件的方法

def copy_file(file, old_folder, new_folder, queue):

# 读取文件

fr = open(old_folder + "/" + file, "rb")

fw = open(new_folder + "/" + file, "wb")

file_size = (os.path.getsize(old_folder + "/" + file)) / 1024 // 1024

print("大小:%d" % file_size + "M|||" + (old_folder + file))

# 文件内容

while True:

content = fr.read(1024 * 1024)

fw.write(content)

if not content:

break

fr.close()

fw.close()

queue.put(file_size)

# 定义一个主方法来操作进程等

def main():

current_path = "/Users/jackyang/Movies/"

# 输入需要拷贝的文件夹

old_folder_name = input("请输入您要拷贝的文件夹:")

old_folder_name = current_path + old_folder_name

# 目标位置的文件夹

new_folder_name = old_folder_name + "-备份"

# 创建文件夹

if os.path.exists(new_folder_name):

os.removedirs(new_folder_name)

else:

os.mkdir(new_folder_name)

# 获取copy目录下所有的文件名

all_file = os.listdir(old_folder_name)

# 创建进程池

pool = Pool(5)

# 创建队列管理用来进程间通信

queue = Manager().Queue()

for file in all_file:

pool.apply_async(copy_file, args=(file, old_folder_name, new_folder_name, queue))

num = 0

total = len(all_file)

# 计算出所有文件的总大小

sum_size = 0

for path in all_file:

sum_size += (os.path.getsize(old_folder_name + "/" + path)) / 1024 // 1024

print("文件总大小:%dM" % sum_size)

current_progress = 0

while num < total:

num += 1

current_progress += queue.get()

copyProgress = current_progress / sum_size

print("当前的进度是:%.f%%" % (copyProgress * 100))

if __name__ == '__main__':

main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值