ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 多线程拷贝单个文件

2019-04-04 16:44:39  阅读:286  来源: 互联网

标签:count threading python flv start import 拷贝 多线程 os


 1 # -*- coding: utf-8 -*-
 2 # @author: Tele
 3 # @Time  : 2019/04/04 下午 12:25
 4 # 多线程方式拷贝单个文件
 5 import threading
 6 import os
 7 import math
 8 
 9 rs = open("F:/ftp_mypc/a.flv", "rb")
10 # 62919061 60MB
11 file_size = os.path.getsize("F:/ftp_mypc/a.flv")
12 if os.path.exists("f:/b/b.flv"):
13     os.remove("f:/b/b.flv")
14 ws = open("f:/b/b.flv", "ab")
15 mutex = threading.Lock()
16 total_count = 0
17 
18 
19 def copy(start, byte_size):
20     # print(threading.current_thread().getName())
21     mutex.acquire()
22     buffer = 1024
23     count = 0
24     rs.seek(start)
25     ws.seek(start)
26     while True:
27         if count + buffer <= byte_size:
28             content = rs.read(buffer)
29             count += len(content)
30             write(content)
31         else:
32             content = rs.read(byte_size % buffer)
33             count += len(content)
34             write(content)
35             break
36     global total_count
37     total_count += byte_size
38     print("\r拷贝进度为%.2f %%" % (total_count * 100 / file_size), end="")
39     mutex.release()
40 
41 
42 def write(content):
43     ws.write(content)
44     ws.flush()
45 
46 
47 def main():
48     # 每个线程拷贝的字节大小
49     per_thread_size = 30000000
50     for i in range(math.ceil(file_size / per_thread_size)):
51         byte_size = per_thread_size
52         if i == math.ceil(file_size / per_thread_size) - 1:
53             byte_size = file_size % per_thread_size
54         start = i * per_thread_size + i
55         t = threading.Thread(target=copy, args=(start, byte_size))
56         t.start()
57 
58     # t1 = threading.Thread(target=copy, args=(0, 30000000))
59     # t2 = threading.Thread(target=copy, args=(30000001, 30000000))
60     # t3 = threading.Thread(target=copy, args=(60000002, 2919061))
61     # t1.start()
62     # t2.start()
63     # t3.start()
64 
65     # 子线程都结束后,释放资源
66     if threading.activeCount() == 1:
67         if ws:
68             ws.close()
69         if rs:
70             rs.close()
71 
72 
73 if __name__ == '__main__':
74     main()

 

标签:count,threading,python,flv,start,import,拷贝,多线程,os
来源: https://www.cnblogs.com/tele-share/p/10655623.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有