# -*- coding: utf-8 -*-
# @author: Tele
# @Time : 2019/04/04 下午 12:25
# 多线程方式拷贝单个文件
import threading
import os
import math rs = open("F:/ftp_mypc/a.flv", "rb")
# 62919061 60MB
file_size = os.path.getsize("F:/ftp_mypc/a.flv")
if os.path.exists("f:/b/b.flv"):
os.remove("f:/b/b.flv")
ws = open("f:/b/b.flv", "ab")
mutex = threading.Lock()
total_count = 0 def copy(start, byte_size):
# print(threading.current_thread().getName())
mutex.acquire()
buffer = 1024
count = 0
rs.seek(start)
ws.seek(start)
while True:
if count + buffer <= byte_size:
content = rs.read(buffer)
count += len(content)
write(content)
else:
content = rs.read(byte_size % buffer)
count += len(content)
write(content)
break
global total_count
total_count += byte_size
print("\r拷贝进度为%.2f %%" % (total_count * 100 / file_size), end="")
mutex.release() def write(content):
ws.write(content)
ws.flush() def main():
# 每个线程拷贝的字节大小
per_thread_size = 30000000
for i in range(math.ceil(file_size / per_thread_size)):
byte_size = per_thread_size
if i == math.ceil(file_size / per_thread_size) - 1:
byte_size = file_size % per_thread_size
start = i * per_thread_size + i
t = threading.Thread(target=copy, args=(start, byte_size))
t.start() # t1 = threading.Thread(target=copy, args=(0, 30000000))
# t2 = threading.Thread(target=copy, args=(30000001, 30000000))
# t3 = threading.Thread(target=copy, args=(60000002, 2919061))
# t1.start()
# t2.start()
# t3.start() # 子线程都结束后,释放资源
if threading.activeCount() == 1:
if ws:
ws.close()
if rs:
rs.close() if __name__ == '__main__':
main()

使用线程池:

 # -*- coding: utf-8 -*-
# @author: Tele
# @Time : 2019/04/04 下午 12:25
# 多线程方式拷贝单个文件,使用concurrent.futures.ThreadPoolExecutor线程池
import threading
import os
import math
from concurrent.futures import ThreadPoolExecutor, wait rs = open("F:/ftp_mypc/a.flv", "rb")
# 62919061 60MB
file_size = os.path.getsize("F:/ftp_mypc/a.flv")
if os.path.exists("f:/b/b.flv"):
os.remove("f:/b/b.flv")
ws = open("f:/b/b.flv", "ab")
mutex = threading.Lock()
total_count = 0 def copy(start, byte_size):
# print(threading.current_thread().getName())
mutex.acquire()
buffer = 1024
count = 0
rs.seek(start)
ws.seek(start)
while True:
if count + buffer <= byte_size:
content = rs.read(buffer)
count += len(content)
write(content)
else:
content = rs.read(byte_size % buffer)
count += len(content)
write(content)
break
global total_count
total_count += byte_size
print("\r拷贝进度为%.2f %%" % (total_count * 100 / file_size), end="")
mutex.release() def write(content):
ws.write(content)
ws.flush() def main():
# 创建线程池
executor = ThreadPoolExecutor(max_workers=3) # 构造参数列表
params_list = list()
per_thread_size = 30000000
for i in range(math.ceil(file_size / per_thread_size)):
byte_size = per_thread_size
if i == math.ceil(file_size / per_thread_size) - 1:
byte_size = file_size % per_thread_size
start = i * per_thread_size + i
params_list.append((start, byte_size)) all_task = [executor.submit(copy, *params) for params in params_list]
# 等待任务完成
wait(all_task)
if ws:
ws.close()
if rs:
rs.close() if __name__ == '__main__':
main()

python 多线程拷贝单个文件的更多相关文章

  1. gulp复制整个文件夹或文件到指定目录(包括拷贝单个文件)

    整个目录: gulp.task('copy', function() { return gulp.src('src/**/*') .pipe(gulp.dest('dist')) }); gulp拷贝 ...

  2. python多线程下载ts文件

    # -*- coding: utf-8 -*- """ Created on Wed Aug 22 15:56:19 2018 @author: Administrato ...

  3. python实现拷贝指定文件到指定目录

    python实现这个功能非常简单,因为库太强大了 import os import shutil alllist=os.listdir(u"D:\\notes\\python\\资料\\&q ...

  4. python 多线程批量传文件

    #!/usr/bin/env python #_*_ coding:utf-8 -*-#autho:leiyong#time:2017-06-05#version: 1.3 import parami ...

  5. Python之FTP多线程下载文件之多线程分块下载文件

    Python之FTP多线程下载文件之多线程分块下载文件 Python中的ftplib模块用于对FTP的相关操作,常见的如下载,上传等.使用python从FTP下载较大的文件时,往往比较耗时,如何提高从 ...

  6. python读取单个文件操作

    python读取单个文件,参考<笨方法学python>的第15节. 运行方式是采用:python python文件名 要读取的文件名 代码中 script, filename = argv ...

  7. python之拷贝文件

    做了个小实验, 用于拷贝文件夹下面的jpg. 用于拓展, 可以引入类和方法, 拷贝你指定的任意类型的文件. import os src = 'C:\\Users\\Administrator\\Des ...

  8. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  9. 进程,线程,GIL,Python多线程,生产者消费者模型都是什么鬼

    1. 操作系统基本知识,进程,线程 CPU是计算机的核心,承担了所有的计算任务: 操作系统是计算机的管理者,它负责任务的调度.资源的分配和管理,统领整个计算机硬件:那么操作系统是如何进行任务调度的呢? ...

随机推荐

  1. 3. Spring Boot Servlet

    转自:https://blog.csdn.net/catoop/article/details/50501686

  2. 91.生成ini文件并写入和读取ini文件

    写入 WritePrivateProfileStringA("hello money", infx[i].name, money, "1.ini"); 按照字符 ...

  3. 在线java反编译服务

    大家是否遇到过有java class文件,却没有java源码的苦恼.近期findmaven.net提供了在线java反编译服务http://www.findmaven.net/decompile_cn ...

  4. IR_drop

    IR压降是指出现在集成电路中电源和地网络上电压下降或升高的一种现象.随着半导体工艺的演进金属互连线的宽度越来越窄,导致它的电阻值上升,所以在整个芯片范围内将存在一定的IR压降.IR压降的大小决定于从电 ...

  5. 【习题 3-12 UVA - 11809】Floating-Point Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] \(A*10^B = temp[M]*2^{2^E-1}\) 两边取一下对数 得到 \(lg_A+B = lg_{temp[M]} ...

  6. HDU 1997汉诺塔VII

    又是汉诺塔~ 回顾一下汉诺塔的移动过程. 从左到右设为A,B,C  3个盘子的时候 1: No.1  A -> C 2: No.2  A -> B 3: No.1  C -> B 4 ...

  7. VS2012 打包部署程序

      一. 下载 InstallShield 2015(支持VS2012) VS2012没有自带打包工具,所以要先下载并安装一个打包工具.我采用微软提供的打包工具:  InstallShield2015 ...

  8. [Nuxt] Load Data from APIs with Nuxt and Vuex

    In a server-rendered application, if you attempt to load data before the page renders and the data f ...

  9. 【3005】拦截导弹问题(noip1999)

    Time Limit: 3 second Memory Limit: 2 MB 某国为了防御帝国的导弹袭击,开发出一种导弹拦截系统,但是这种拦截系统有一个缺陷:虽然他的第一发炮弹能达到任意的高度,但是 ...

  10. Linux 如何查看一个进程的堆栈

    有两种方法: 第一种:pstack 进程ID 第二种,使用gdb 然后attach 进程ID,然后再使用命令 thread apply all bt 第三种:strace -f -p pid  该方法 ...