# -*- 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. Java – Reading a Large File Efficiently--转

    原文地址:http://www.baeldung.com/java-read-lines-large-file 1. Overview This tutorial will show how to r ...

  2. 【几何/数学】概念的理解 —— (非)刚体变换((non-)rigid transformation)

    1. 刚体变换与非刚体变换 What is a non-rigid transformation? 刚体变换(rigid transformation)一般分为如下几种: 平移对象,而不改变形状和大小 ...

  3. Java核心技术 卷Ⅰ 基础知识(3)

    第五章 继承 继承已存在的类就是复用这些类的方法和域.反射是指在程序运行期间发现更多的类及其属性的能力. . 反射 . 使用反射编写泛型数组代码 继承设计的技巧

  4. HDU 2473 Junk-Mail Filter 并查集删除(FZU 2155盟国)

    http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~ ...

  5. REGEXP_LIKE,REGEXP_INSTR,REGEXP_SUBSTR,REGEXP_REPLACE

    参考: http://www.cnblogs.com/scottckt/archive/2012/10/11/2719562.html http://www.jb51.net/article/3842 ...

  6. [Debug] Chrome Devtools: Elements - Console Integration

    The Element Inspector in Chrome DevTools offers powerful integration with the console - learn how to ...

  7. ZOJ 1796 Euchre Results 数学水题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1796 题意: 四个人玩游戏,已知三个人的输赢情况,求第四个人的输赢情况. ...

  8. 【例题3-1 UVA - 272 】TEX Quotes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 大水题. 用一个int记录遇到的是左括号还是右括号. [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] #inc ...

  9. 修复STS4 server中没有Tomcat的问题(必看,官方推荐,包教包会,国内首发)

    版权声明:原创.欢迎转载,转载请注明来源,谢谢. https://blog.csdn.net/qq_41910280/article/details/83279129 修复STS4 server中没有 ...

  10. word中公式的排版及标题列表

    1.首先建好你的标题,如标题1,标题2等等,你能够依次改变它们的字体,段落等格式,新建格式例如以下图所看到的 红圈处即建立新的格式,你能够建立不论什么你想要的格式,非常方便: 2.当你建立好了多个标题 ...