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是计算机的核心,承担了所有的计算任务: 操作系统是计算机的管理者,它负责任务的调度.资源的分配和管理,统领整个计算机硬件:那么操作系统是如何进行任务调度的呢? ...
随机推荐
- 1.1 Introduction中 Producers官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Producers 生产者(Producers) Producers publish ...
- (转) 如何取消Linux下,vi中显示的^M符号
转自:http://www.cnblogs.com/dkblog/archive/2012/02/03/2337187.html [背景知识] ^M 是ascii中的'\r', 回车符,是16进制的0 ...
- Mongodb总结1-启动和Shell脚本
2013年,还在秒针,当时听说了Mongodb,就学习了下,搞了下HelloWorld.主要是熟悉Mongodb的启动.命令行的Shell脚本.Java访问的CRUD. 今天,由于需要,再次回顾和进一 ...
- nginx+tomcat 架构 HttpServletRequest.getScheme()获取正确的协议
http://blog.csdn.net/ofofw/article/details/46791447
- 微服务实战(二):使用API Gateway - DockOne.io
原文:微服务实战(二):使用API Gateway - DockOne.io [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用 ...
- 数据结构-堆实现优先队列(java)
队列的特点是先进先出.通常都把队列比喻成排队买东西,大家都非常守秩序,先排队的人就先买东西. 可是优先队列有所不同,它不遵循先进先出的规则,而是依据队列中元素的优先权,优先权最大的先被取出. 这就非常 ...
- Swift UIView 层次调整
Swift 中添加的UIView都是有层级的. 我们先添加三个看一看 let view1=UIView(frame: CGRectMake(10, 50, 200, 200)) let view2=U ...
- RTC时钟和BKP的配置stm32
摘自:https://blog.csdn.net/gtkknd/article/details/52233605 RTC和后备寄存器通过一个开关供电,在VDD有效的时候选择VDD供电,否则选择VBAT ...
- Codeforces Round #Pi (Div. 2) B Berland National Library
B. Berland National Library time limit per test1 second memory limit per test256 megabytes inputstan ...
- bootstrap课程3 bootstrap中常用的排版样式有哪些
bootstrap课程3 bootstrap中常用的排版样式有哪些 一.总结 一句话总结:bootstrap里面对常用表情比如p.h1.code等html中的常用表情都修改了样式,照着手册用就好,样式 ...