python 多线程拷贝单个文件
# -*- 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 多线程拷贝单个文件的更多相关文章
- gulp复制整个文件夹或文件到指定目录(包括拷贝单个文件)
整个目录: gulp.task('copy', function() { return gulp.src('src/**/*') .pipe(gulp.dest('dist')) }); gulp拷贝 ...
- python多线程下载ts文件
# -*- coding: utf-8 -*- """ Created on Wed Aug 22 15:56:19 2018 @author: Administrato ...
- python实现拷贝指定文件到指定目录
python实现这个功能非常简单,因为库太强大了 import os import shutil alllist=os.listdir(u"D:\\notes\\python\\资料\\&q ...
- python 多线程批量传文件
#!/usr/bin/env python #_*_ coding:utf-8 -*-#autho:leiyong#time:2017-06-05#version: 1.3 import parami ...
- Python之FTP多线程下载文件之多线程分块下载文件
Python之FTP多线程下载文件之多线程分块下载文件 Python中的ftplib模块用于对FTP的相关操作,常见的如下载,上传等.使用python从FTP下载较大的文件时,往往比较耗时,如何提高从 ...
- python读取单个文件操作
python读取单个文件,参考<笨方法学python>的第15节. 运行方式是采用:python python文件名 要读取的文件名 代码中 script, filename = argv ...
- python之拷贝文件
做了个小实验, 用于拷贝文件夹下面的jpg. 用于拓展, 可以引入类和方法, 拷贝你指定的任意类型的文件. import os src = 'C:\\Users\\Administrator\\Des ...
- Python 多线程教程:并发与并行
转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...
- 进程,线程,GIL,Python多线程,生产者消费者模型都是什么鬼
1. 操作系统基本知识,进程,线程 CPU是计算机的核心,承担了所有的计算任务: 操作系统是计算机的管理者,它负责任务的调度.资源的分配和管理,统领整个计算机硬件:那么操作系统是如何进行任务调度的呢? ...
随机推荐
- 学习笔记:_lodash.js常用函数2
_.pick(object, [props]) 创建一个从object中选中的属性的对象. 示例: var object = { 'a': 1, 'b': '2', 'c': 3 }; _.pick( ...
- js取对象的属性值循环
var data = {name: "liuyang", job: "web", age: "27"} Object.keys(data). ...
- 调色板原理 & 编程
调色板原理 & 编程 逻辑调色板结构LOGPALETTE,该结构定义如下: typedef struct tagLOGPALETTE { WORD palVersion; //调色板的板本号, ...
- 【CS Round #43 D】Bad Triplet
[链接]点击打开链接 [题意] 给你n个点m条边的无权无向联通图; 让你找3个点A,B,C 使得A->B=B->C=A->C 这里X->Y表示点X到点Y的最短路长度. [题解] ...
- maven的pom.xml配置文件讲解
<project xmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.w3.o ...
- 一个Java8模型的batch队列
有点小问题,cpu过高,但是思路不错: http://www.tuicool.com/articles/URz2i2q
- FMS2015:NVMe SSD的高可靠性及数据保护
FMS2015是一个充满技术干货的平台,各领域技术大拿在峰会上分享的技术和产品都影响甚至主导着闪存下一阶段的发展. 此次Memblaze的project师团队也是从存储系统.PCIe SSD以及闪存控 ...
- Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)
Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...
- (转)RMAN-06054: media recovery requesting unknown archived log for thread...
转自:http://blog.itpub.net/29800581/viewspace-1307267/ 使用rman执行recover database 的时候出现RMAN-06054的错误提示: ...
- 【codeforces 434 div 1 A】Did you mean...
[链接]h在这里写链接 [题意] 让你维护一段序列. 这段序列,不会出现连续3个以上的辅音. (或者一块全是辅音则也可以) (用空格可以断开连续次数); 要求空格最小. [题解] 模拟着,别让它出现连 ...