# -*- 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. libcurl 上传文件至 web服务器

    测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php) libcurl vc6 工程代码  下载地址:  http://download.csdn.ne ...

  2. Java Web学习总结(13)——JSP入门

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  3. 【算法导论-36】并查集(Disjoint Set)具体解释

    WiKi Disjoint是"不相交"的意思.Disjoint Set高效地支持集合的合并(Union)和集合内元素的查找(Find)两种操作,所以Disjoint Set中文翻译 ...

  4. 如何使用 PyCharm 将代码上传到GitHub上(详细图解)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一丶说明 测试条件:需要有GitHub账号以及在本地安装了Git工具,无论是Linux环境还是Windows都是一样的 如果还没有 ...

  5. springMVC easyUI filebox 单个文件上传

    被这个文件上传坑到如今.还是自己技术问题,照着之前extjs项目那边的上传实例,愣是上传不了 到后面就查了下springMVC的文件上传,依照那样搞定了http://blog.csdn.net/jad ...

  6. google校招在线測试题---2048

    先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵) #include<iostream> #include<fstream&g ...

  7. Windows Vista下UIPI和Windows消息的故事(UIPI默认情况下会禁止所有高于WM_USER的消息)

    一. 什么是UIPI UIPI指User Interface Privilege Isolation(用户界面特权隔离),是Windows Vista 新引入的一种安全特性.用于拦截接收比自身进程MI ...

  8. HDU 1405 The Last Practice 数学水题

    http://acm.hdu.edu.cn/showproblem.php?pid=1405 题目大意: 给你一个数,让你分解素因子,输出它的各次幂. 如60 输出:2 2 3 1 5 1 (60=2 ...

  9. 云应用开发之新浪SAE读写云端数据库MySQL

    本博文为前篇博文新浪云应用SAE日志查看的延续. 在读写云数据库MySQL之前,须要说明的是,在新浪云平台上使用数据库时.该平台默认会为每个应用单独新建一个数据库database实例.在该实例中再创建 ...

  10. 使用ionic3快速开发webapp(一)

    Ionic可以让我们使用web技术快速构建接近原生体验的跨平台移动应用. 一.安装ionic 1.需要先安装 Node.js(版本8.x之上): 2.安装cordova 和 ionic: $ npm ...